- PHP Basics
- PHP - Introduction
- PHP - Install
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo or Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP – Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Advanced
- PHP - Operators
- PHP - Control Statements
- PHP - Arrays
- PHP - Functions
- PHP - Superglobals
- PHP - File Handling
- PHP- Object Oriented Programming
- PHP - AJAX
- PHP - Web Development
- PHP - Enums
PHP Syntax
PHP (Hypertext Preprocessor) is a widely-used open source scripting language especially suited for web development and can be embedded into HTML. Here are some basics of PHP syntax to get you started:
Basic PHP Syntax
-
PHP Tags:
PHP tags are used to delineate PHP code within a file. These tags tell the server to interpret the enclosed code as PHP. Here are the different types of PHP tags you can use.
Standard PHP Tags:The most common way to open and close PHP code:
PHP
// Your PHP code goes here
Short-Open Tags:These tags are a shorter form but are not always enabled by default on all servers due to potential security issues. They can be enabled with the short_open_tag directive in the php.ini file:
PHP
// Your PHP code goes here
Echo Tags:A shorthand for '<?php echo' that directly outputs content:
PHP
"Hello, World!";ASP Tags:These tags are similar to ASP style and are rarely used. They require the asp_tags directive to be enabled in the php.ini file (which is deprecated and removed in recent PHP versions):
PHP
// Your PHP code goes here
Script Tags:Another rarely used method, similar to embedding JavaScript, and requires asp_tags to be enabled:
PHP
<script language="php">
// PHP code goes here
</script> -
Echo Statement: Used to output text or variables.
PHP
echo "Hello, World!"
-
Variables.
Variables in PHP start with a$
sign followed by the name of the variable.PHP
$greeting "Hello,World!";
echo $greeting
-
Comments.
Single-line comments:PHP
// This is a single-line comment
echo "Hello, World!" // This is also a single-line commentMulti-line comments:PHP
/*
This is a multi-line comment
*/
echo "Hello, World!" -
Data Types:
String:PHP
$String "Hello,World!"
echo $String
Integer:PHP
$int = 42;
echo $int
Float:PHP
$float 3.14
echo $float
Boolean:PHP
$bool = true
echo $bool
-
Arrays:
Indexed array:PHP
$colors array("Red", "Green", "Blue");echo $colors[0]; // Outputs "Red"
Associative array:PHP
$ages= array("Peter" => 35, "Ben" => 37, "Joe" => 43);echo $ages["Peter" ]; // Outputs 35
-
Conditional Statements:
If statement:PHP
$x = 10 ;if ($x > 5) {
echo "x is greater than 5";
}
If-else statement:PHP
$x = 10 ;if ($x > 5) {
echo "x is greater than 5";
} else{
echo "x is not greater than 5";
}
Elseif statement:PHP
$x = 10 ;if ($x > 5) {
echo "x is greater than 5";
} elseif ($x == 5){
echo "x is equal to 5";
} else{
echo "x is not greater than 5";
}
-
Loops:
While loop:PHP
$x = 1 ;while ($x <=5) {
echo "The number is: $x <br>";
}
For loop:PHP
for( $x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
Foreach loop:PHP
$colors = array( "Red", "Green", "Blue");foreach ($colors as $value) {
echo "$value <br>";
}
-
Functions:
PHP
function writeMessage() {echo "Hello, World!";
}writeMessage(); // Calls the function
-
Superglobals:
$_GET
,$_POST
,$_SERVER
,$_SESSION
,$_COOKIE
,$_FILES
,$_REQUEST
,$_ENV
PHP
echo $_SERVER['PHP_SELF']; // Outputs the filename of the currently executing script
Example of a Simple PHP Script
Here's a small example combining several of these elements:
php<?php
// Define variables
$name = "John Doe";
$age = 30;
$is_student = true;
// Function to display greeting
function displayGreeting($name) {
echo "Hello, $name!";
}
// Check age and display appropriate message
if ($age > 18) {
echo "You are an adult.<br>";
} else {
echo "You are a minor.<br>";
}
// Loop through an array of colors
$colors = array("Red", "Green", "Blue");
foreach ($colors as $color) {
echo "Color: $color <br>";
}
// Call the function
displayGreeting($name);
?>