Sunteți pe pagina 1din 40

 PHP Hypertext Preprocessor.

 Created by Resmos Lerdorf


 Other Names : Personal Home Page,
Professional Home Page
 “PHP is a server-side scripting language
designed specifically for the Web.
 HTML generates the web page with the static
text and images.
 PHP has the need to evolved for dynamic web
based application, mostly involving database
usage.
 PHP involves
 simplicity in scripting
 platform independence.
 PHP is
 primarily designed for web applications
 well optimized for the response times needed
for web applications
 Is an open source.
 Easy to use
 Easy to learn
 PHP can generate dynamic page content
 PHP can collect form data
 PHP can add, delete, modify data in your database
 PHP can be used to control user-access
 PHP can encrypt data
 Before you continue you should have a basic
understanding of the following:
 HTML
 CSS
 JavaScript
 PHP code block is embedded within the
<?php and ?> tags.
 When the server encounters the PHP tags it
switches from the HTML to PHP mode.
 There are four different ways to embed the
PHP code
 <?php echo(“Some PHP code”); ?>
 <? echo(“Some PHP code”); ?>
<html>
<head>
<title>My personal Hello World! PHP script</title>
</head>
<body>
<?php
echo “Hello World!”;
?> PHP tag, allow to insert PHP
code. Interpretation by PHP
</html> module will substitute the
code with code output
 To display things on the page, we've used:
print( ).
echo( ).
 <?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
 To use or assign variable $ must be present
before the name of the variable.
 Example:
$num1
$salary
The assign operator is '=‘
There is no need to declare the type of the
variable
 $num1 = 100;
 $salary = 250;
 $total = 350;
 $coats1 = "Winter Coats";
 $coats1 = 'Winter Coats';
A variable can be used before to be
assigned
$A = 1;
$B = ‘2’;
$C = ($A + $B);
echo $C;
 You can join together direct text, and
whatever is in your variable.
 The full stop (period or dot, to some) is used
for this.
Example:
$2b=‘apple’;
$2c=‘orange’;
echo($2b . $2c);
<?php
$first_number = 10;
$direct_text = 'My variable contains the value
of';
echo($direct_text . $first_number);
?>
- A string is a sequence of characters

$stringTest = “this is a sequence of chars”;


echo $stringTest[0];
echo $stringTest;
A single quoted strings is displayed “as-is”
$age = 37;
$stringTest = 'I am $age years old';
Concatenation
$conc = ”is “.”a “.”composed
“.”string”;
echo $conc;
$newConc = 'Also $conc '.$conc;
echo $newConc;
 To add up the contents of variables, you just
separate each variable name with a plus
symbol.
 Example:
<?php
$num1= 3;
$num2= 5;
$total= $num1 + $num2;
echo($total);
?>
<?php
$first_number = 10;
$second_number = 20;
$sum_total = $first_number + $second_number;
$direct_text = 'The two variables added
together = ';
echo ($direct_text . $sum_total);
?>
 Subtraction is more or less the same.
 Instead of the plus sign (+), simply use the minus
sign (-).
 Change your $sum_total line to this, and run
your code:

$sum_total = $second_number-$first_number;
<?php
$first_number = 10;
$second_number = 20;
$third_number = 100;
$sum_total = $third_number -
$second_number - $first_number;
echo($sum_total);
?>
 To multiply in PHP (and just about every other
programming language), the * symbol is used.
 If you see 20 * 10, it means multiply 20 by 10.

 Here's some code for you to try:

$sum_total = $second_number * $first_number;


See if you can guess what the answer is before
trying it out:
<?php
$first_number = 10;
$second_number = 2;
$third_number = 3;
$sum_total = $third_number +
$second_number * $first_number;
echo ($sum_total);
?>
 To divide one number by another, the /
symbol is used in PHP. If you see 20 / 10, it
means divide 10 into 20.
<?php
$first_number = 10;
$second_number = 20;
$sum_total = $second_number / $first_number;
echo ($sum_total);
?>
 Again, you have to be careful of operator
precedence. Try this code:
<?php
$first_number = 10;
$second_number = 20;
$third_number = 100;
$sum_total = $third_number - $second_number
/ $first_number;
echo ($sum_total);
?>
<?php echo $add, " " , $sub, " ";
echo $mult," ", $div, " ";
$a = 10; echo "\n";
$b = 11;
$c = 12; ?>

$add = $a + $b +$c;
$sub = $c - $a;
$mult = $a * $b;
$div = $c / 3;
PHP Assignment Operators

 The PHP assignment operators are used 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.
Assignment Same Description
as...
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
$y = 20; $i = 5;
$y += 100; $i *= 6;
echo $y; echo $i;

$z = 50; $j = 10;
$z -= 25; $j /= 5;
echo $z; echo $j;
 IF statement
if (<condition>) {
//php code goes here
}
else {
//php code goes here
}
 Alternative Syntax
if(<condition>) :
//html code goes here
else :
//html code goes here
endif;
 Looping statement are the statements
execute one or more statement repeatedly
several number of times.
 Types of Looping statement
 while
 do, while
 for
 foreach
 The while loop executes a block of code as
long as the specified condition is true.
 Syntax:
while(condition){
code to be executed
}
 The do, while loop will always execute the
block of code once, it will then check the
condition, and repeat the loop while the
specified condition is true.
 Syntax:
do{
code to be executed
}
while(condition);
 The for loop is used when you know in
advance how many times the script should
run.
 Syntax
for (initial value; condition; counter) {
code to be executed;
}
 The foreach loop works only on arrays, and is
used to loop through each key/value pair in
an array.
 Syntax
foreach ($array as $value) {
code to be executed;
}

S-ar putea să vă placă și