Basic PHP syntax
1.Opening and Closing Tags:
PHP code is enclosed within opening and closing tags.
The standard opening tag is <?php, and the closing tag is ?>
PHP code should be written between these tags.
2.Variables:
PHP variables start with the $ symbol, followed by the variable name.
Variable names are case-sensitive and can contain letters, numbers, and underscores.
Variables do not require explicit type declaration.
Example: $name = “John”;
3.Comments:
PHP supports single-line and multi-line comments.
Single-line comments start with // or #.
Multi-line comments are enclosed between /* and /. Example: phpCopy code // This is a single-line comment / This is a multi-line comment */
4.Outputting Content:
To display content on the webpage, you can use the echo or print statements.
Example:
echo “Hello, World!”; print “Welcome to PHP!”;
5.Data Types:
PHP supports various data types, including:
Strings: Enclosed in single quotes (‘) or double quotes (“).
Integers: Whole numbers without decimal points.
Floats: Numbers with decimal points.
Booleans: true or false.
Arrays: Ordered collections of values.
Objects: Instances of classes.
Null: Represents the absence of a value.
6.Operators:
PHP supports various operators, including arithmetic, assignment, comparison, logical, and string concatenation operators.
Example:
$num1 = 10; $num2 = 5; $sum = $num1 + $num2; // Arithmetic operator (+)
$is True = ($num1 > $num2); // Comparison operator (>)
7.Control Structures:
PHP provides control structures for conditional execution and looping.
Examples:
if-else statement:
if ($age >= 18)
{ echo “You are an adult.”; }
else { echo “You are minor.”; }
for loop:
for ($i = 1; $i <= 10; $i++) { echo $i . ” “; }
8.Functions:
PHP allows you to define and call functions.
Example:
function greet($name)
{ echo “Hello, ” . $name . “!”; }
greet(“John”); // Function call
9.Arrays:
PHP supports indexed arrays and associative arrays.
Example: $fruits = array(“Apple”, “Banana”, “Orange”); // Indexed array
$person = array( “name” => “John”, “age” => 30, “country” => “USA” ); // Associative array
10.Include and Require:
PHP allows you to include or require external PHP files within your script.
include “header.php”;
require “functions.php”;