Learn PHP programming from scratch with our comprehensive tutorial
PHP (Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
A PHP script can be placed anywhere in the document. A PHP script starts with <?php and ends with ?>.
<?php
// PHP code goes here
?>
Variables are containers for storing data. In PHP, a variable starts with the $ sign, followed by the name of the variable.
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
In PHP, there are two basic ways to get output: echo and print. Both are used to output data to the screen.
<?php
echo "Hello World!";
print "Hello World!";
?>
PHP supports the following data types:
<?php
$string = "Hello World!";
?>
<?php
$int = 5985;
?>
<?php
$float = 10.365;
?>
<?php
$bool = true;
?>
<?php
$array = array("Volvo", "BMW", "Toyota");
?>
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("black", "Volvo");
?>
Control structures are used to control the flow of execution in a program.
<?php
if ($a > $b) {
echo "a is greater than b";
}
?>
<?php
if ($a > $b) {
echo "a is greater than b";
} else {
echo "b is greater than a";
}
?>
<?php
switch ($color) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
default:
echo "Your favorite color is neither red nor blue!";
}
?>
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
<?php
$i = 1;
while ($i <= 5) {
echo "The number is: $i <br>";
$i++;
}
?>
<?php
$i = 1;
do {
echo "The number is: $i <br>";
$i++;
} while ($i <= 5);
?>
<?php
for ($i = 1; $i <= 5; $i++) {
echo "The number is: $i <br>";
}
?>
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
A function is a block of statements that can be used repeatedly in a program.
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg();
?>
<?php
function familyName($fname) {
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
?>
<?php
function sum($x, $y) {
return $x + $y;
}
echo "5 + 10 = " . sum(5, 10);
?>
An array stores multiple values in one single variable.
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2]."<br>";
?>
Superglobals are built-in variables that are always available in all scopes.
<?php
echo "Hello " . $_GET['name'];
// URL: page.php?name=John
?>
<?php
echo "Hello " . $_POST['name'];
// Form submission with name field
?>
Regular expressions are patterns used to match character combinations in strings.
<?php
// Check if string contains "php"
if (preg_match("/php/i", "PHP is fun!")) {
echo "Match found!";
}
// Replace all occurrences of "php" with "PHP"
$str = "Visit php!";
$str = preg_replace("/php/i", "PHP", $str);
echo $str;
?>