Tutorial PHP Arrays

An array stores multiple values in one single variable:

<?php
$cars=array(“red”,”blue”,”blue”);
echo “I like ” . $cars[0] . “, ” . $cars[1] . ” and ” . $cars[2] . “.”;
?>

What is an Array?

An array is a special variable, which can hold more than one value at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

$cars1=”red”;
$cars2=”blue”;
$cars3=”blue”;

Create an Array in PHP

In PHP, the array() function is used to create an array:

array();

Get The Length of an Array – The count() Function

<?php
$cars=array(“Volvo”,”BMW”,”Toyota”);
echo count($cars);
?>
3starinfo.com

PHP If…Else Statements

PHP Conditional Statements

3starinfo.com
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

  • if statement – executes some code only if a specified condition is true
  • if…else statement – executes some code if a condition is true and another code if the condition is false
  • if…else if….else statement – selects one of several blocks of code to be executed
  • switch statement – selects one of many blocks of code to be executed

 

PHP – The if Statement

The if statement is used to execute some code only if a specified condition is true.

Syntax

if (condition)
{
code to be executed if condition is true
;
}
<?php
$a=date(“H”);
if ($a<“10”)
{
echo “hello”;
}
?>

PHP – The if…else Statement

Use the if….else statement to execute some code if a condition is true and another code if the condition is false.

Syntax

if (condition)
{
executed if condition is true;
}
else
{
executed if condition is false;
}

Example

<?php
$t=date(“H”);
if ($t<“20”)
{
echo “Have a good day!”;
}
else
{
echo “Have a good night!”;
}
?>

PHP – The if…else if….else Statement

Syntax

if (condition)
{
code to be executed if condition is true;
}
else if (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if condition is false;
 }
<?php
$t=date(“H”);
if ($t<“10”)
{
echo “Have a good morning!”;
}
else if ($t<“20”)
{
echo “Have a good day!”;
}
else
{
echo “Have a good night!”;
}
?>

The PHP Switch Statement

Use the switch statement to select one of many blocks of code to be executed.

Syntax

switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}

Example

<?php
$color=”red”;
switch ($color)
{
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, or green!”;
}
?>

PHP Operators

The assignment operator = is used to assign values to variables in PHP

The arithmetic operator + is used to add values together in PHP.

PHP Arithmetic Operators

Operator Name Description Example Result
x + y Addition Sum of x and y 2 + 2 4
x – y Subtraction Difference of x and y 5 – 2 3
x * y Multiplication Product of x and y 5 * 2 10
x / y Division Quotient of x and y 15 / 5 3
x % y Modulus Remainder of x divided by y 5 % 2
10 % 8
10 % 2
1
2
0
– x Negation Opposite of x – 2
a . b Concatenation Concatenate two strings “Hi” . “Ha” HiHa

PHP Assignment Operators

The basic assignment operator in PHP is “=”. It means that the left operand gets set to the value of the expression on the right. That is, the value of “$x = 5” is 5.

Assignment Same as… Description
x = y x = y The left operand gets set to the value of the expression on the right
x += y x = x + y Addition
x -= y x = x – y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus
a .= b a = a . b Concatenate two strings

PHP Incrementing/Decrementing Operators

Operator Name Description
++ x Pre-increment Increments x by one, then returns x
x ++ Post-increment Returns x, then increments x by one
— x Pre-decrement Decrements x by one, then returns x
x — Post-decrement Returns x, then decrements x by one

PHP Comparison Operators

Comparison operators allows you to compare two values:

Operator Name Description Example
x == y Equal True if x is equal to y 5==8 returns false
x === y Identical True if x is equal to y, and they are of same type 5===”5″ returns false
x != y Not equal True if x is not equal to y 5!=8 returns true
x <> y Not equal True if x is not equal to y 5<>8 returns true
x !== y Not identical True if x is not equal to y, or they are not of same type 5!==”5″ returns true
x > y Greater than True if x is greater than y 5>8 returns false
x < y Less than True if x is less than y 5<8 returns true
x >= y Greater than or equal to True if x is greater than or equal to y 5>=8 returns false
x <= y Less than or equal to True if x is less than or equal to y 5<=8 returns true

PHP Logical Operators

Operator Name Description Example
x and y And True if both x and y are true x=6
y=3
(x < 10 and y > 1) returns true
x or y Or True if either or both x and y are true x=6
y=3
(x==6 or y==5) returns true
x xor y Xor True if either x or y is true, but not both x=6
y=3
(x==6 xor y==3) returns false
x && y And True if both x and y are true x=6
y=3
(x < 10 && y > 1) returns true
x || y Or True if either or both x and y are true x=6
y=3
(x==5 || y==5) returns false
! x Not True if x is not true x=6
y=3
!(x==y) returns true

PHP Array Operators

Operator Name Description
x + y Union Union of x and y
x == y Equality True if x and y have the same key/value pairs
x === y Identity True if x and y have the same key/value pairs in the same order and are of the same type
x != y Inequality True if x is not equal to y
x <> y Inequality True if x is not equal to y
x !== y Non-identity True if x is not identical to y

PHP Variable Scopes

The scope of a variable is the part of the script where the variable can be referenced/used.

PHP has four different variable scopes:

  • local
  • global
  • static
  • parameter

Local Scope

example:

<?php
$x=5; // global scope

function my()
{
echo $x; // local scope
}

my();
?>

Global Scope

<?php
$x=5; // global scope
$y=10; // global scope

function my()
{
global $a,$b;
$c=$a+$b;
}

my();
echo $c; // outputs 15
?>

Static Scope

When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted.

<?php

function my()
{
static $a=0;
echo $a;
$a++;
}

my();
my();
my();

?>

Parameter Scope

A parameter is a local variable whose value is passed to the function by the calling code.

Example

<?php

function myTest($x)
{
echo $x;
}

myTest(5);

?>