Showing posts with label 2-Getting started. Show all posts
Showing posts with label 2-Getting started. Show all posts

PHP Operators

All programming languages use some operators to manipulate operations on variables and values such as assign, multiply, add, subtract and concatenate them, the operators may be between a variable and a value, or between two values, or between two variables.
Operators have the form of symbols (such as + and -) and combinations of symbols (such as ++ and +=).

Arithmetic operators:
The Arithmetic operators are:

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (division remainder)
++ Increment
-- Decrement

PHP Code & examples:
<?php
$var1 = 25 + 5 ; // returns 30
$var2 = $var1 / 5 ; // returns 6
$var3 = $var1 + $var2 ; // returns 36
?>

Assignment operators:
The Assignment operators are used to facilitate assigning values to variables:

Operator Description
+= Addition and Assignment
-= Subtraction and Assignment
*= Multiplication and Assignment
/= Division and Assignment
% Modulo and Assignment
.= Concatenation and Assignment

PHP Code & examples:
<?php
$var = 20 ;

$var = $var + 5 ; // returns 25
// We can use the addition and assignment
$var += 5 ; // returns 25
//these two methods return the same result, but as you see, the second is shorter and better !
?>

Comparison Operators:
The Comparison Operators returns a value true or false after comparing two values:

Operator Description
== is equal to
!= is not equal
>
is greater than
< is less than
>= is greater than or equal ro
<= is less than or equal to

the comparaison operators are the most of time used in conditions:
<?php
$var = 20 ;
if ($var1==$var2)
// this instruction will be executed only if $var1 is equal to $var2
if ($var1!=$var2)
// this instruction will be executed only if $var1 is not equal to $var2
if ($var1<$var2)
// this instruction will be executed only if $var1 is less than $var2

// and this is also possible:
$var = ($var1==$var2);
?>

Logical Operators:
The logical Operators are used in conditions, they make a relation between two or more condition. Logical operations are also know as Boolean operators:

Operator Description
&& And: returns true only if two operands (conditions/expressions) are trues
||
Or: returns true if at least one of the two operands returns true
!
Not: if the operand is true, returns false, else returns true
xor
Xor: returns true if only one of the operands returns true. If the two operands are true, it returns false.

Increment and decrement Operators:
The logical Operators are used in conditions, they make a relation between two or more condition. Logical operations are also know as Boolean operators:

Operator Description
++$var Increments the variable value before it is used in rest of expression
--$var
Decrements the variable value before it is used in rest of expression
$var++
Increments the variable value after it is used in rest of expression
$var--
Decrements the variable value after it is used in rest of expression

PHP Echo, the relation between php and html

Echo is the most used php function to make php interact with html. In fact, echo is used to write some text into a html page, or may be in a text file, or any file that contain text.

<?php
echo "wellcome !";
?>
Display:
Wellcome !
And in html:

<html>
<head>
<title>My Page Title here</title>
</head>
<body>
<?php
echo "Hello World ! ";
?>
</body>
</html>
Display:

<html>
<head>
<title>My Page Title here</title>
</head>
<body>
Hello World !
</body>
</html>
Echo and variables
You can echo both variables or directly a text, this example will show you how:

Output a variable:
<?php
$myvar = "Wellcome !";
echo $myvar;
?>
display
Wellcome !


Output directly a text:
<?php
echo "Wellcome !";
?>
Display
Wellcome !

As you see, we have the same displaying text, no difference between putting a text or a variable.

What type of variables can I echo ?
Again, in php there are no types of variables, you can output anythings, strings, numbers, arrays ...
PHP will convert automatically numbers and other type to text so they can be displayed in html pages.

PHP Variables

The most known difference between php and other languages, is that in php we don't need to declare variables, we just use them and they are automatically declared and with the right type !

What are Variables ?
Variable are some area, block or memory space to store data, a value such a text or number or array or anything else.

How to use Variables in php?
In our script, we can use variables over and over and this will not make any problem, and using variables is in two ways:
-Writing to a variable: we can write to a variable some value, such texts (strings) or numbers, and we can also write to a variable that don't exist, php will declare it automatically with the right type.
This is how we write to variable:
<?php
$mystring = 'Hello world';
$mynumber = 2008 ;
?>
And to read from a variable:
<?php
$mystring = 'Hello world';
$mymessage = $mystring ; // This will copy the value of mystring to mymessage
$mynumber = 2008 ;
?>
PHP variables and types:
In php there are no types for variables, that why we don't have to declare them. For example if we write a number into a variable that contains string, this variable will be converted automatically to a number variable.
<?php
$myvar = 'Hello world'; // This variable contain a string
$myvar = 2008 ; // The same variable converted to number
?>

PHP Syntax

Statement and semicolons in php.
In php, and like most languages, at the end of each php instuction, we must add a semicolon, for example:
<?php
echo "Hello Jack, ";
echo "You are in our member page ! ";
?>
Display :
Hello Jack, You are in our member page !
Comments in php.
To add comments to your php script, we use // for one line comments, or both /* and */ for multiline and large block comments.
<?php
// Here one line comment
echo "Hello Jack, ";

/* Here
large block
comment
*/
echo "You are in our member page ! ";
?>
Spaces in php.
You can as spaces as you want in your php script, this will not affect in anyway your code, and they will not be displayed in your html page, only spaces between " and " or ' and ' will be displayed in your html page.
<?php
echo "Hello Jack, ";
echo "You are in our member page ! ";
?>
This will not make any difference in the page, it will be displayed:
Hello Jack, You are in our member page !
next post: PHP variables.