Chapter 4 : PHP Web Applications
Chapter 4 PHP (Hypertext Preprocessor) : Syntax
In the case of dynamic pages, when you type an address in your browser, the query (PHP code) will be
sent to the server. The web server processes the PHP code and returns the HTML page to the client.
There, the browses interpret the HTM code.
1. Instructions and comments
-PHP instructions could coexist with the HTML tags in the same file. So those instructions have to be
delimited by the start and end PHP tags:
<?php : start PHP
?> : end PHP code
-A line which starts with // or # is considered as a comment, same is the sentence written between /*
and */
2. PHP variables
Each variable must start with the $ followed by letters and could contain numbers or the underscore _.
Examples :
- $var, $_num, $_10club,$var1_2 , $name, $10, $k-1
3. Constants
To define a constant , we use the term “define” :
bool define ($name , $value)
Example :
define (“Pi”,3.14);
4. special characters
\n or \r new line
\t tabulation
\\ back slash
\$ the $ character
Mohamed Hedi ElHajjej Page 1
Chapter 4 : PHP Web Applications
\" the " character
5. Operators
Assignments: =, += ,-=,*=,/=,++, --
mathematic: + - / * %
comparison: == != < > <= >=
logic: && or AND ,|| or OR , XOR , !
concatenation: .
6. Functions
- function definition
function my_function_name(type1 parameter1,type2 parameter2, type3 parameter3){
//instructions
}
- function call
my_function( value1, value2, value3);
if the function is written in a separated PHP file, we have to call it first.
Example :
Let a function factorial defined in a file called “ functions.php”. Then to call it, we have to write the
following code:
<?php
include('functions.php');
echo 'the factorial of 7 is '.factorial(7);
?>
When a function returns a value, we can save it in a variable like this
definition
int sum (int $a, int $b)
{ return $a+$b; }
call
$s=sum(198,3);
7. echo function
- echo „ 2011‟ ; or echo "2011";
- echo $var;
- echo „today is „.$day ; or echo "today is $day";
We can include HTML tags in an echo function:
echo „<b>course</b>‟ ;
or do the inverse
<input type= "submit " value="<?php echo ( $val ) ;?>">
Mohamed Hedi ElHajjej Page 2