PHP Tutorial

The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications. This tutorial helps you to build your base with PHP.

What is PHP?

One of the most useful JavaScript HTML method is the getElementByID().

  • PHP is an acronym for "PHP: Hypertext Preprocessor"
  • PHP is a widely-used, open source scripting language
  • PHP scripts are executed on the server
  • PHP is free to download and use

View Sample

PHP Variables

The main way to store information in the middle of a PHP program is by using a variable. Variables are "containers" for storing information.

View Sample

PHP Syntax

A PHP script can be placed anywhere in the document. A PHP script starts with <?php and ends with ?>:

<?php
// PHP code goes here
?>

Example

<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>

View Sample

Creating (Declaring) PHP Variables

PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.

Example

<!DOCTYPE html>
<html>
<body>
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>

After the execution of the statements above, the variable $txt will hold the value Hello world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.

View Sample

Rules for PHP Variables

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)
View Sample

PHP Variables Scope

In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes:

  • local
  • global
  • static
View Sample

GLobal Scope

There are two types of values in Javacript

  • variable declared outside a function
  • can only be accessed outside a function
View Sample

Local Scope

  • variable declared within a function
  • can only be accessed within that function

Example

<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}

myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
</body>
</html>

View Sample

PHP echo and print Statements

With PHP, there are two basic ways to get output: echo and print. The echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.

<!DOCTYPE html>
<html>
<body>
<?php
print "<h2>PHP is Fun!</h2>";
print "print<br>";
echo "echo";
?>
</body>
</html>

View Sample

PHP Data Types

Variables can store data of different types, and different data types can do different things. PHP supports the following data types:

  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource
View Sample

PHP String

A string can be any text inside quotes. You can use single or double quotes:

<!DOCTYPE html>
<html>
<body>
<?php
$x = "Hello, this a string!";

echo $x;
?>
</body>
</html>

View Sample

PHP Integer

An integer data type is a non-decimal number. They are whole numbers, without a decimal point, like 4195.

Rules for Integers:

  • An integer must have at least one digit
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation

In the following example $x is an integer. The PHP var_dump() function returns the data type and value:

<!DOCTYPE html>
<html>
<body>
<?php
$x = 5985;
var dump($x);
?>
</body>
</html>

View Sample

PHP Float

A float (floating point number) is a number with a decimal point or a number in exponential form.

Example

<!DOCTYPE html>
<html>
<body>
<?php
$x = 10.365;
var dump($x);
?>
</body>
</html>

View Sample

PHP Array

An array stores multiple values in one single variable. In the following example $cars is an array. The PHP var_dump() function returns the data type and value:

Example

<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
</body>
</html>

View Sample

PHP Operators

Operators are used to perform operations on variables and values. PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators
View Sample

PHP Boolean

A Boolean represents two possible states: TRUE or FALSE. Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.

View Sample

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

View Sample

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Example

<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x == $y); // returns true because values are equal
?>
</body>
</html>

Example

<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x != $y); // returns false because values are equal
?>
</body>
</html>

View Sample

PHP Conditional Statements

Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements:

  • if statement - executes some code if one condition is true
  • if...else statement - executes some code if a condition is true and another code if that condition is false
  • if...elseif...else statement - executes different codes for more than two conditions
  • switch statement - selects one of many blocks of code to be executed
View Sample

PHP - The if Statement

The if statement executes some code if one condition is true.

Example

<!DOCTYPE html>
<html>
<body>
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
</body>
</html>

View Sample

PHP - The if...else Statement

The if...else statement executes some code if a condition is true and another code if that condition is false.

Example

<!DOCTYPE html>
<html>
<body>
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}else {
echo "Have a good night!";
}
?>
</body>
</html>

View Sample

PHP switch Statement

The switch statement is used to perform different actions based on different conditions.

Example

<!DOCTYPE html>
<html>
<body>
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
</body>
</html>

View Sample