Professional Documents
Culture Documents
Outline
What is PHP?
History of PHP
Why PHP ?
What is PHP file?
What you need to start using PHP ?
Syntax PHP code .
echo & print Statement
Variables.
Data Types.
Constants &Operators.
Conditional Statements & Loops.
HTTP usage logging and server-side form generation in Unix. PHP runs on different platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc.
PHP has support for a wide range of databases
PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols
(SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans . PHP is free. Download it from the official PHP resource: www.php.net
PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was
PHP is easy to learn and runs efficiently on the server side
renamed the Zend Engine. Many security features were added.
PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2
library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP
1
9/17/2014
What does PHP code look like? What Can PHP Do?
Structurally similar to C/C++ PHP can generate dynamic page content
PHP can create, open, read, write, and close files on the server
With PHP you are not limited to output HTML. You can output images, PDF
files, and even Flash movies. You can also output any text, such as XHTML and
XML
6. www.php.net/manual/ en/install.php
2
9/17/2014
print is not actually a real function (it is a Parameters: echo can take more than one parameter when print only takes one parameter.
used without parentheses. The syntax is echo
expression [, expression[, expression] ... ]. Note
language construct) so you are not required to that echo ($arg1,$arg2) is invalid.
Variables Variables
As with algebra, PHP variables can be used to hold values (x=5) or A variable name must begin with a letter or the underscore character
expressions (z=x+y). A variable name can only contain alpha-numeric characters and
Variable can have short names (like x and y) or more descriptive names underscores (A-z, 0-9, and _ )
Rules for PHP variables: Variable names are case sensitive ($y and $Y are two different variables)
A variable starts with the $ sign, followed by the name of the variable
3
9/17/2014
After the execution of the statements above, the variable txt will hold the
value Hello world!, and the variable xwill hold the value 5. ?>
Note: When you assign a text value to a variable, put quotes around the
value.
PHP automatically converts the variable to the correct data type, $name = 'elijah';
depending on its value. $yearborn = 1975;
In a strongly typed programming language, we will have to declare (define)
$currentyear = 2005;
the type and name of the variable before using it.
$age = $currentyear - $yearborn;
echo ("$name is $age years old.");
?>
4
9/17/2014
</strong> </p>
Parameter
string variables are used for values that contain characters. Single quoted strings will display things almost completely "as is." Variables and most escape
sequences will not be interpreted. The exception is that to display a literal single quote, you
After we have created a string variable we can manipulate it. A string can be used
can escape it with a back slash \', and to display a back slash, you can escape it with another
directly in a function or it can be stored in a variable. backslash \\ (So yes, even single quoted strings are parsed).
In the example below, we create a string variable called txt, then we assign the text
"Hello world!" to it. Then we write the value of the txt variable to the output:
<?php <?php
$txt = my string ; $txt = my string ;
PHP strings can be specified in four ways PHP strings can be specified in four ways
Double quote strings will display a host of escaped characters (including some regexes), and Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator,
variables in the strings will be evaluated. An important point here is that you can use curly an identifier is provided, then a newline. The string itself follows, and then the same
braces to isolate the name of the variable you want evaluated. For example let's say you have identifier again to close the quotation. You don't need to escape quotes in this syntax.
the variable $type and you what to echo "The $types are" That will look for the Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The
variable $types. To get around this use echo "The {$type}s are" You can put the left brace difference is that not even single quotes or backslashes have to be escaped. A nowdoc is
before or after the dollar sign. Take a look at string parsing to see how to use array variables identified with the same <<< sequence used for heredocs, but the identifier which follows is
and such. enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.
<?php <?php
$txt = my string; $txt = my string ;
?> ?>
5
9/17/2014
PHP strings can be specified in four ways PHP strings can be specified in four ways
My name is "$name".
My name is "MyName". I am printing some A Now, I am printing some A Now,
I am printing some {A}. I am printing some {A}. This should print a capital 'A': \x41
This should print a capital 'A': A
<?php <?php
echo Hello world <br>;
$word = World;
echo Hello world;
echo Hello $word <br>;
?>
echo Hello $word <br>;
?>
// or # for single line You cant have any whitespace between <? and
/* */ for multiline php.
/*
You cant break apart keywords (e.g :whi le,func
this is my comment one
tion,fo r)
this is my comment two
this is my comment three
You cant break apart varible names and function
*/ names (e.g:$var name,function f 2)
6
9/17/2014
This is true for both variables and character escape-sequences (such as \n or \\)
7
9/17/2014
$testString = 10.2abc;
<?php // call function settype to convert variable
?>
?>
8
9/17/2014
-x Negation Opposite of x -2
x *= y x=x*y Multiplication
$a - $b // subtraction
x /= y x=x/y Division
$a * $b // multiplication
x %= y x=x%y Modulus
$a / $b // division
a .= b a=a.b Concatenate two strings $a += 5 // $a = $a+5 Also works for *= and /=
echo ++$a;
x ++ Post-increment Returns x, then increments x by one
// output 3,$a is now equal to 3
?>
9
9/17/2014
This function displays structured information about one or more expressions that
<?php includes its type and value. Arrays and objects are explored recursively with values
indented to show structure.
?>
x === y Identical True if x is equal to y, and they are of same 5==="5" returns false var_dump(0 == "a"); // 0 == 0 -> true boolean true
type boolean false
var_dump("1" != "01"); // 1 != 1 -> false boolean true
x != y Not equal True if x is not equal to y 5!=8 returns true
boolean false
x <> y Not equal True if x is not equal to y 5<>8 returns true var_dump("10" == "1e1"); // 10 == 10 -> true boolean false
boolean true
x !== y Not identical True if x is not equal to y, or they are not of 5!=="5" returns true
same type var_dump("10" == "1ee1"); // 10 == 1 -> false
x>y Greater than True if x is greater than y 5>8 returns false
var_dump(100 === "100"); // 100 == 100 -> false
x<y Less than True if x is less than y 5<8 returns true
var_dump("100" === "100"); // 100 == 100 -> 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
10
9/17/2014
x && y And True if both x and y are true x=6 $f = false or true;
y=3
(x < 10 && y > 1) returns true
var_dump($e, $f);
x || y Or True if either or both x and y are true x=6
y=3 $g = true && false;
(x==5 || y==5) returns false
!x Not True if x is not true x=6 $h = true and false;
y=3
!(x==y) returns true var_dump($g, $h);
?>
11
9/17/2014
<?php
<?php $a =10;
$a =10; $b = &$a;
$b =$a; $b= 20;
$b =20 echo $a; // 20
Echo $a; // 10 ?>
?>
12
9/17/2014
13
9/17/2014
14
9/17/2014
If multiple parameters are supplied then isset() will return TRUE only if all of the
{
parameters are set. Evaluation goes from left to right and stops as soon as an echo "This var is set so I will print.";
unset variable is encountered. }
?>
The behavior of unset() inside of a function can vary depending on what type of $foo = 'bar';
variable you are attempting to destroy.
echo $foo;
If a globalized variable is unset() inside of a function, only the local variable is
destroyed. The variable in the calling environment will retain the same value as
unset($foo);
before unset() was called.
echo $foo;
?>
a:
phpinfo(); echo 'Bar';
?>
<?php
?> for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto end;
}
}
echo "i = $i";
end:
echo 'j hit 17';
?>
15
9/17/2014
<?php
if ($foo == 0) {
echo The variable foo is equal to 0;
Chapter Example }
else if (($foo > 0) && ($foo <= 5)) {
echo The variable foo is between 1 and 5;
}
else {
echo The variable foo is equal to .$foo;
}
?>
?>
16
9/17/2014
Do..While For..If
<?php
<?php $rows = 4;
$i = 0; echo '<table><tr>';
do { for($i = 0; $i < 10; $i++){
17
9/17/2014
<?php
//this is a different way to use the 'for' <?php
//Essa uma maneira diferente de usar o 'for' for($a=0;$a<10;$a++){
for($i = $x = $z = 1; $i <= 10;$i++,$x+=2,$z=&$p){
for($b=0;$b<10;$b++){
$p = $i + $x; for($c=0;$c<10;$c++){
for($d=0;$d<10;$d++){
echo "\$i = $i , \$x = $x , \$z = $z <br />";
echo $a.$b.$c.$d.", ";
} }
}
?>
}
}
?>
If - Switch Do..While - IF
<?php
$i = 1;
if ($i == 0) { <?php
echo "i equals 0"; do {
} elseif ($i == 1) {
echo "i equals 1";
if ($i < 5) {
} elseif ($i == 2) { echo "i is not big enough";
echo "i equals 2"; break;
} }
switch ($i) { $i *= $factor;
case 0: if ($i < $minimum_limit) {
echo "i equals 0"; break;
break;
case 1:
}
echo "i equals 1"; echo "i is ok";
break;
case 2: /* process i */
echo "i equals 2";
break;
} } while (0);
?> ?>
18
9/17/2014
If in other style
<?php
$hour = 11;
?>
19