PHP Tags: PHP code are embedded within HTML, and it's enclosed within tags. For example:
<?php
// PHP code goes here
?>
Comments: Comments in PHP can be either single-line (//) or multi-line (/* */). They are ignored by the PHP interpreter and are used for documentation or to add notes within the code.
// This is a single-line comment
/*
This is a
multi-line comment
*/
Variables: Variables in PHP start with the dollar sign $, followed by the variable name. Variable names are case-sensitive and can contain letters, numbers, and underscores but cannot start with a number.
$name = "John";
$age = 25;
Data Types: PHP supports various data types, including integers, floats, strings, booleans, arrays, objects, and NULL.
$num = 10; // integer
$price = 10.5; // float
$name = "John"; // string
$is_active = true; // boolean
$fruits = array("apple", "banana", "orange"); // array
Output: You can output data to the browser using the echo or print statements.
echo "Hello, World!";
print "Hello, World!";
String Concatenation: You can concatenate strings using the . operator.
$greeting = "Hello, " . $name;
echo $greeting;
Arrays: PHP supports both indexed arrays and associative arrays.
$numbers = array(1, 2, 3, 4, 5); // Indexed array
$person = array("name" => "John", "age" => 25); // Associative array
Control Structures: PHP supports common control structures like if-else statements, loops, and switch-case statements.
// If-else statement
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
// While loop
$i = 0;
while ($i < 5) {
echo $i;
$i++;
}
// Switch-case statement
$day = "Monday";
switch ($day) {
case "Monday":
echo "Today is Monday.";
break;
case "Tuesday":
echo "Today is Tuesday.";
break;
default:
echo "Today is not Monday or Tuesday.";
}
Functions: You can define functions in PHP using the function keyword.
function greet($name) {
echo "Hello, $name!";
}
greet("John");
Include and Require: You can include external PHP files using the include or require statements.
include "functions.php";
require "constants.php";
These are the fundamental elements of PHP syntax that you'll encounter frequently. As you continue learning, you'll explore more advanced topics and concepts. Practice coding regularly and refer to the PHP documentation for detailed explanations and examples.