Ethical Hacking Programming, Blogging, Hosting, All Computer Software, PC Software Download, JAVA in hindi, HTML, PHP, C, C++, Free Learning, Software's Download, Technical Videos, Technical Tricks and Tips, How Make Money

PHP Reference: Beginner to Intermediate PHP chapter 2 Class 1

Operators

When comparing or processing variables and other values you use operators. Without them, PHP would be more of a word jumble instead of a language. In some unique cases, operators slightly alter the relationship between two variables or their function within PHP. Without further adieu, here they are.



Basic Operators

Add ( + ): $a = 1; $a = $a + 5; // $a is equal to 6

Subtract ( - ): $s = 10; $s = $s - 5; // $s is equal to 5
Multiply ( * ): $m = 2; $m = $m * 10; // $m is equal to 20

Divide ( / ): $d = 20; $d = $d / 5; // $d is equal to 4
Modulus ( % ) Provides the remainder after division:

$u = 5; $u = $u % 2; // $u is equal to 1



Assignment Operators

Add ( += ): $a = 1; $a += 5; // $a is equal to 6

Subtract ( -= ): $s = 10; $s -= 5; // $s is equal to 5
Multiply ( *= ): $m = 2; $m *= 10; // $m is equal to 20

Divide ( /= ): $d = 20; $d /= 5; // $d is equal to 4
Modulus ( %= ) Provides the remainder after division:

$u = 5; $u %= 2; // $u is equal to 1

Concatenate ( .= ) Join onto the end of a string:
$c = 5; $c .= 2; // $c is now a string, '52'

See Also:

Concatenate – Join together in succession


Comparison Operators
Greater Than ( > ): 2 > 1
Less Than ( < ): 1 < 2
Greater Than or Equal To ( >= ): 2 >= 2 3 >= 2
Less Than or Equal To ( <= ): 2 <= 2 2 <= 3

Short-Hand Plus or Minus one

Also known as:
Increment ( $integer++; )

Decrement ( $integer--; )

Example:

$a = 1;

$a = $a + 1; // $a is now equal to 2 $a++; // $a is now equal to 3
$a--; // $a is now equal to 2 again, same as $a = $a – 1;



@ - Suppress Errors

Placing the commercial at symbol (@) before a function tells PHP to suppress any errors generated by that function.

Examples:

include('DoesNotExist.txt');

Warning: include(DoesNotExist.txt) [function.include]: failed to open
stream: No such file or directory

@include('DoesNotExist.txt');

// blank output below because the error was suppressed





& - Pass by Reference

References allow two variables to refer to the same content. In other words, a variable points to its content (rather than becoming that content). To pass from context, two variables allow to indicate the same content under different names. Apersander (&) is placed in front of the referencing variable.
Examples:

$a = 1;
$b = &$a; // $b references the same value as $a, currently 1

$b = $b + 1; // 1 is added to $b, which effects $a the same way echo "b is equal to $b, and a is equal to $a";

b is equal to 2, and a is equal to 2





Use this for functions when you want to change only the original variable and want to re-double it with your new value again in the same variable name.

function add(&$var){ // The & is before the argument $var $var++;
}

$a = 1;
$b = 10;

add($a);
echo "a is $a,";
add($b);
echo " a is $a, and b is $b"; // Note: $a and $b are NOT referenced

a is 2, a is 2, and b is 11

You can also do this to alter an array with foreach:

$array = array(1,2,3,4);

foreach ($array as &$value){
$value = $value + 10;
}

unset ($value); // Must be included, $value remains after foreach loop print_r($array);

Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 )



Ternary Operator

The Ternary Operator is a short-hand form for evaluating what to do when an expression is evaluated as either TRUE or FALSE. The conditional returns either the TRUE or FALSE output. Basic format is as follows:

(expr) ? ValueIfTrue : ValueIfFalse ;

Examples:

$boolean = TRUE;

$result = ($boolean) ? 'Is True' : 'Is False'; echo $result;

Is True

// $result is not yet set

$result = (isset($result)) ? $result+1 : 10; echo " \$result = $result.";

$result = (isset($result)) ? $result+1 : 10; echo " \$result = $result.";


$result = 10. $result = 11.

The Equal Sign

Assignment ( = ): Assigns the value on the right to the variable on the left
Equality ( == ): Checks if the left and right values are equal

Identical ( === ): Checks if the left and right values are equal AND identical


Example:

$a = 1; // Sets the value of $a as 1 by assignment

$b = TRUE; // Sets the value of $b to the boolean TRUE if ($a == $b){
echo 'a is equal to b.';

}
if ($a === $b){

echo 'a is identical and equal to b.';
}


a is equal to b.



Not ( ! ), Not Equal to ( != ), Not Identical to ( !== )

Used in conditional statements to evaluate as true a FALSE result of an expression or if a value is NOT equal to the second value.

Example:

$a = 1;

if (!isset($a)){ // If the variable $a is NOT set then...

echo '$a is not set'; // The expression is TRUE if it is NOT set // Since there is no ELSE statement, nothing is displayed
}
if ($a != 0){

echo '$a does not equal zero';
}


$a does not equal zero

See The Equal Sign above for equality versus identical



Concatenate (The Period)

A period is used to join dissimilar items as part of a string in the same order as they are listed. In many cases this is used to reference the value of a function or of an array, which cannot be referenced within double quotations ( "" ) when being assigned to a $string variable.

Example:

$array = array( 1 => 'Hello' );
$string = 'World';

echo '$string in single quotes, followed by ' . $array[1] . "$string";


$string in single quotes, followed by HelloWorld


Comparison Operators (non-arithmetic)

and ( && )
or ( || )

xor ( xor ) - Or, but not All


Examples:

if (1 == 1 && 2 == 2){

echo 'And is True';
}


And is True

if (1 == 1 || 2 == 2){

echo 'At least one of these is True';
}


At least one of these is True

if (1 == 1 xor 2 == 10){

echo 'One of these is True, but not both';
}


One of these is True, but not both

Share:

Follow On YouTube