In this article, we’ll delve into the core concepts of PHP basics, including syntax, variables, constants, and operators, accompanied by illustrative code examples.
Syntax and Structure
PHP follows a straightforward syntax that resembles other programming languages like C, Java, and Perl. Here’s a brief overview of PHP syntax and structure:
1. PHP Tags
PHP code is enclosed within <?php and ?> tags. Anything outside these tags is treated as HTML or plain text.
2. Comments
PHP supports single-line comments starting with // and multi-line comments enclosed within /* */.
// This is a single-line comment
/*
This is a
multi-line comment
*/
3. Statements
Statements in PHP end with a semicolon (;). PHP doesn’t require statements to be terminated with semicolons, but it’s considered a best practice for readability and maintainability.
$name = "John"; // Statement 1
$age = 25; // Statement 2
4. Output
You can output text or variables using the echo or print statements.
echo "Hello, World!";
print "Hello, World!";
Variables and Data Types
Variables in PHP are containers for storing data values. PHP supports various data types, including integers, floats, strings, booleans, arrays, and objects.
1. Declaring Variables
Variables in PHP start with the dollar sign ($) followed by the variable name.
$name = "John";
$age = 25;
2. Data Types
PHP automatically assigns data types to variables based on the assigned value. You can also explicitly specify data types using type declarations (available in PHP 7 and later).
$name = "John"; // String
$age = 25; // Integer
$height = 5.9; // Float
$isStudent = true; // Boolean
$fruits = array("Apple", "Banana", "Orange"); // Array
Constants
Constants in PHP are like variables, but their values cannot be changed once defined. Constants are useful for storing values that remain constant throughout the script’s execution.
1. Defining Constants
Constants in PHP are defined using the define() function.
define("PI", 3.14);
define("SITE_NAME", "My Website");
2. Accessing Constants
You can access constants using their names, prefixed with the constant() function or the :: scope resolution operator (for class constants).
echo PI; // Outputs: 3.14
echo constant("PI");// Outputs: 3.14
Operators
Operators in PHP are symbols that perform operations on variables and values.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
$x = 10;
$y = 5;
echo $x + $y; // Addition
echo $x - $y; // Subtraction
echo $x * $y; // Multiplication
echo $x / $y; // Division
echo $x % $y; // Modulus (remainder)
2. Assignment Operators
Assignment operators are used to assign values to variables.
$x = 10; // Assigns the value 10 to $x
$x += 5; // Equivalent to $x = $x + 5
$x -= 5; // Equivalent to $x = $x - 5
3. Comparison Operators
Comparison operators are used to compare two values.
$x = 10;
$y = 5;
echo ($x == $y); // Equal to
echo ($x != $y); // Not equal to
echo ($x > $y); // Greater than
echo ($x < $y); // Less than
4. Logical Operators
Logical operators are used to combine conditional statements.
$x = 10;
$y = 5;
echo ($x > 0 && $y > 0); // AND
echo ($x > 0 || $y > 0); // OR
echo !($x > 0); // NOT
Conclusion
Understanding PHP basics lays a solid foundation for mastering web development with PHP. In this article, we explored the syntax and structure of PHP, learned about variables and data types, discovered constants and their usage, and explored various operators for performing operations on values. Armed with this knowledge, you’re well-equipped to dive deeper into PHP programming and build dynamic web applications.
This simple PHP script outputs “Hello, World!” when executed. It demonstrates the basic syntax of PHP using the echo statement to print text to the browser.