PHP Language Basics

Web developers can produce dynamic content that communicates with databases using the PHP Hypertext Preprocessor (PHP), a computer language. PHP is widely used for web apps. Rasmus Lerdorf released PHP‘s initial version in 1994. “PHP” stands for “PHP: Hypertext Preprocessor” and is a recursive acronym.Lexical
HTML incorporates PHP, a server-side programming language. Databases, dynamic content, session monitoring, and e-commerce websites can be managed using it. The popular databases MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server are compatible with PHP. PHP is said to execute pleasantly quickly, particularly when compiled as an Apache module on Unix platforms. Once launched, the MySQL server is renowned for swiftly processing intricate queries with sizable result sets.
PHP’s practical nature is influenced by a number of significant features, including simplicity, efficiency, security, flexibility, and familiarity. PHP aims to be as forgiving as possible and has a syntax similar to C.
Common Uses and Basic Syntax
PHP is capable of creating, opening, reading, writing, and closing files on a system in addition to managing databases and dynamic content. Managing forms via collecting data, storing data in a file, emailing data, and returning data to the user are examples of common usage. Additionally, it can read and set cookie variables, encrypt data, limit user access to website pages, and add, remove, and edit objects inside your database.
A straightforward “Hello, World!” script is frequently used as the starting point to demonstrate the fundamental grammar. PHP statements are included with regular HTML in special markup elements since PHP is incorporated in HTML. is the PHP tag style that works the best everywhere. Other accepted tags are ASP-style tags <%…%> and short-open tags , albeit they may need special php.ini file configuration options to be enabled. Additionally supported are HTML script tags .
Before being delivered to the client, all PHP code enclosed in these tags is parsed by the server’s PHP Parser and removed from the page. The web browser just receives the HTML output that is produced. The script that follows serves as an illustration of how to incorporate PHP in HTML:
<!DOCTYPE html>
<html>
<head>
<title>Hello PHP</title>
</head>
<body>
<h1>Our First PHP Program</h1>
<?php
echo "Hello, world!"; // This is a PHP statement
?>
<p>This text is plain HTML.</p>
</body>
</html>
The browser would display Hello, World! as a result of this script.
Commenting and Whitespace
A program’s comments are sections that are only meant for human readers and are eliminated before the program’s output is shown. There are two primary commenting forms that PHP supports:
- Single-line comments: These are used to provide brief justifications pertinent to local code. They begin with // or #.
<?php
# This is a single-line comment
// This is another single-line comment
print "Hello"; // Comment at the end of a line
?>
- Comments with many lines: used for pseudocode methods or more thorough explanations. Like C, they are included by /* and */.
<?php
/* This is a multi-line comment
It can span across multiple lines.
Author: Tutorialspoint */
print "This line is not commented";
?>
Because PHP is whitespace insensitive, it usually doesn’t matter how many spaces, tabs, or carriage returns are used between elements. Whitespace characters are handled the same way as a single character. PHP is case sensitive, though. $capital and $CaPiTaL, for example, would be considered distinct variables.
Statements and Expressions
In PHP, a statement is any expression that has a semicolon (;) after it. Any series of legitimate PHP statements encapsulated in PHP tags constitutes a valid PHP program. $greeting = “Welcome to PHP!”; is an example statement that assigns a string to a variable. Combinations of tokens the simplest PHP building blocks like variables or numbers create expressions.
Variables and Data Types

The main way that data is stored in a PHP program is through variables. In PHP, a leading dollar symbol ($) is used to identify each variable. A variable’s most recent assignment determines its value. The assignment operator (=), with the variable on the left and the expression to be evaluated on the right, is used to assign values to variables.
Before being given a value, variables do not need to be declared. One important feature of PHP is that variables lack intrinsic types; that is, they are unsure of whether they will carry a string or a number. When required, PHP can automatically convert data types. Default values are used for variables used prior to assignment. Perl-like is how PHP variables are described.
Eight data types are supported by PHP when creating variables:
- Integers: Whole numbers, like 4195, that do not have a decimal point. They can be either positive or negative and are the most basic kind.
- Doubles: Numbers with floating points, such as 49.1 or 3.14159. They use decimal points to represent numbers.
- Booleans: Only TRUE or FALSE are possible values. TRUE and FALSE are constants in PHP that can be used as Booleans.
- NULL: A unique type that has just one value. Although it is case-insensitive, the constant NULL is typically capitalised. When tested with IsSet(), a variable assigned NULL returns FALSE and evaluates to FALSE in a Boolean context.
- PHP supports string operations.’ is an example of a string. Strings are defined with single or double quotations (“…”). Double-quoted strings interpret escape sequences such \n for newline, \r for carriage return, \t for tab, \$ for dollar sign, \” for double quotation, and \ for backslash. The “here document” idiom assigns multiple lines to a string variable.
- Arrays: Collections of other values with names and indexes.
- Objects: Programmer-defined class instances that are able to bundle class-specific functions and values.
- Resources: Unique variables that contain mentions of outside resources, such as database connections.
While arrays and objects are complex types that can hold other values of any types, the first five are simple types.
Variable Scope
One of four scope types is available for PHP variables:
- Local variables: Variables declared inside a function can only be referenced within that function due to their local scope. A variable that is assigned to outside of the function is regarded as an entirely distinct variable.
- The output shows that the $x inside and outside the function are different.
- Function parameters: These act as local variables inside the function’s scope and are defined inside parenthesis after the function name.
- The example demonstrates how the multiply function uses and modifies the parameter $value.
- Global variables: Anywhere in the application, you can access these variables. However, the GLOBAL keyword must be used to explicitly designate a global variable as global in order to change it within a function. PHP is instructed to use the existing global variable with that name by this keyword.
- The example demonstrates that the global $somevar can be modified by using GLOBAL $somevar; inside addit().
- Static variables
Constants
A constant is a name or identification for a basic value that remains unchanged while a script is running. Constants are case-sensitive and usually have uppercase identifiers. Constant names must have letters, digits, or underscores after the first. A constant cannot be undefined or altered after it has been defined. Constants may only hold scalar data types, which include boolean, integer, float, and string.
The define() function is used to define constants. Constants do not need a leading dollar symbol ($), in contrast to variables. They are not bound by variable scoping constraints and can be defined and accessed anywhere in the script.
<?php
define("MINSIZE", 50);
echo MINSIZE; // Output: 50
echo constant("MINSIZE"); // Also outputs: 50
?>
Additionally, PHP has a number of predefined “magic constants” whose values vary according to their usage.