PHP (Hypertext Preprocessor) is a widely-used open-source scripting language that is especially suited for web development. It allows you to embed code within HTML and execute it on the server side. Understanding the core syntax of PHP is essential for any developer looking to work with this powerful language.
In this tutorial, we will cover the basic syntax of PHP, including how to write and structure your PHP scripts. We'll start by setting up a simple PHP environment and then dive into key concepts like comments, variables, data types, operators, control structures, and functions.
A PHP script is executed on the server, and the output is sent to the browser as plain HTML. The basic structure of a PHP script includes opening (<?php) and closing (?>) tags. Anything between these tags is considered PHP code.
<?php
// Your PHP code goes here
?>
PHP supports single-line comments, multi-line comments, and inline comments:
Single-line comments start with //:
// This is a single-line comment
Multi-line comments are enclosed within /* */:
/*
* This is a multi-line comment.
* It can span multiple lines.
*/
Inline comments start with # (though this is less common):
# This is an inline comment
Variables in PHP are denoted by the $ symbol followed by the variable name. Variable names must start with a letter or underscore and can only contain letters, numbers, and underscores.
<?php
$greeting = "Hello, World!";
echo $greeting; // Outputs: Hello, World!
?>
PHP supports several data types:
String: A sequence of characters enclosed in quotes.
<?php
$name = "Alice";
?>
Integer: Whole numbers without a decimal point.
<?php
$age = 25;
?>
Float: Numbers with a decimal point.
<?php
$height = 5.9;
?>
Boolean: Represents true or false values.
<?php
$isStudent = true;
?>
Array: An ordered map of key-value pairs.
<?php
$fruits = array("apple", "banana", "cherry");
?>
Object: Instances of classes.
<?php
class Car {
public $color;
function __construct($color) {
$this->color = $color;
}
}
$myCar = new Car("red");
?>
NULL: Represents a variable with no value.
<?php
$nullVar = null;
?>
PHP supports various operators for arithmetic, comparison, logical, string, and array operations.
<?php
$a = 10;
$b = 5;
echo $a + $b; // Addition: 15
echo $a - $b; // Subtraction: 5
echo $a * $b; // Multiplication: 50
echo $a / $b; // Division: 2
echo $a % $b; // Modulus: 0
?>
<?php
$a = 10;
$b = 5;
var_dump($a == $b); // Equal: bool(false)
var_dump($a != $b); // Not equal: bool(true)
var_dump($a > $b); // Greater than: bool(true)
var_dump($a < $b); // Less than: bool(false)
var_dump($a >= $b); // Greater than or equal to: bool(true)
var_dump($a <= $b); // Less than or equal to: bool(false)
?>
<?php
$a = true;
$b = false;
var_dump($a && $b); // And: bool(false)
var_dump($a || $b); // Or: bool(true)
var_dump(!$a); // Not: bool(false)
?>
<?php
$str1 = "Hello";
$str2 = " World";
echo $str1 . $str2; // Concatenation: Hello World
echo $str1 . ", " . $str2 . "!"; // Concatenation with comma and space: Hello, World!
?>
<?php
$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);
$result = $array1 + $array2; // Union: array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6)
?>
PHP supports various control structures for decision-making and looping.
<?php
$age = 18;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "It's Monday!";
break;
case "Tuesday":
echo "It's Tuesday!";
break;
default:
echo "It's another day.";
}
?>
<?php
for ($i = 0; $i < 5; $i++) {
echo "Number: " . $i . "<br>";
}
?>
<?php
$i = 0;
while ($i < 5) {
echo "Number: " . $i . "<br>";
$i++;
}
?>
<?php
$i = 0;
do {
echo "Number: " . $i . "<br>";
$i++;
} while ($i < 5);
?>
Functions in PHP are defined using the function keyword. They can take parameters and return values.
<?php
function greet($name) {
return "Hello, " . $name . "!";
}
echo greet("Alice"); // Outputs: Hello, Alice!
?>
Let's put together a simple PHP script that demonstrates some of the concepts we've covered.
<?php
// Define variables
$name = "John";
$age = 30;
// Function to display information
function displayInfo($name, $age) {
echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
}
// Call the function
displayInfo($name, $age);
// Conditional statement
if ($age >= 18) {
echo "You are an adult.<br>";
} else {
echo "You are a minor.<br>";
}
// Loop to display numbers
for ($i = 1; $i <= 5; $i++) {
echo "Number: " . $i . "<br>";
}
?>
To run this script, save it with a .php extension (e.g., example.php) and place it in your web server's document root. Then, access it through your browser by navigating to http://localhost/example.php.
In the next section, we will delve deeper into variables in PHP, exploring how to declare, manipulate, and use them effectively in your scripts.
Stay tuned for more tutorials on PHP!