01.introduction To PHP
01.introduction To PHP
1331204 PABI
Science Calculations
System
System
C uses curly
braces { }
for code
blocks.
Scripting/
Interpreted
http://en.wikipedia.org/wiki/
History_of_programming_languages
PHP
• PHP is an acronym for "PHP: Hypertext
Preprocessor”
• PHP is a widely-used, open source scripting
language (Interpreted Language)
• PHP scripts are executed on the server
• PHP is free to download and use
<h1>Hello from Dr. Chuck's HTML Page</h1>
<p>
<?php
echo "Hi there.\n";
$answer = 6 * 7;
echo "The answer is $answer, what ";
echo "was the question again?\n";
?>
</p>
<p>Yes another paragraph.</p>
PHP from the Command Line
http://php.net/manual/en/
language.variables.basics.php
Strings
http://php.net/manual/en/
language.types.string.php
<?php
Double
echo "this is a simple string\n"; Quote
// Outputs: Variables do 12
$expand = 12;
echo "Variables do $expand\n";
<?php Single
echo 'this is a simple string'; Quote
http://php.net/manual/en/language.basic-
syntax.comments.php
Output
<?php
$x = "15" + 27;
echo($x); 42
echo("\n");
?>
Expressions
• Increment / Decrement ( ++ -- )
• String concatenation ( . )
• Equality ( == != )
• Identity/Identical ( === !== ) (Same information and same
datatype)
• Ternary ( ? : )
• Null Coalescing Operator (??) (PHP 7+)
• Side-effect Assignment ( += -= .= etc.)
• Ignore the rarely-used bitwise operators ( >> << ^ | & )
Increment / Decrement
$x = 12;
$y = 15 + $x++; x is 13 and y is 27
echo "x is $x and y is $y \n";
$x = 12;
$y = 15 + ++$x; x is 13 and y is 28
echo "x is $x and y is $y \n";
Increment / Decrement
$x = 12;
$y = 15 + $x; x is 13 and y is 27
$x = $x + 1;
echo "x is $x and y is $y \n";
$x = 12;
$y = 15 + ($x + 1); x is 13 and y is 28
echo "x is $x and y is $y \n";
String Concatenation
$www = 123;
$msg = $www > 100 ? "Large" : "Small" ;
echo "First: $msg \n";
$msg = ( $www % 2 == 0 ) ? "Even" : "Odd";
echo "Second: $msg \n"; First: Large
$msg = ( $www % 2 ) ? "Odd" : "Even";
Second: Odd
echo "Third: $msg \n";
Third: Odd
Side-Effect Assignment
echo "\n";
$out = "Hello";
$out = $out . " ";
$out .= "World!";
$out .= "\n"; Hello World!
echo $out; Count: 1
$count = 0;
$count += 1;
echo "Count: $count\n";
Null Coalescing Operator
This operator returns its first operand if it is set and not NULL .
Otherwise it will return its second operand.
$x = null;
$x = $x ?? 3; x = 3
$x = 4;
$x = $x ?? 3; x = 4
$x = null;
$x ??= 3; x = 3
Conversion / Casting
X: 125 X: 125
Y: 10025 Y: 10025
Z: 25 Traceback:"cast.py", line 5
z = int("sam") + 25;
ValueError: invalid literal
Casting
The concatenation operator
tries to convert its operands to
strings.
echo "A".FALSE."B\n"; TRUE becomes an integer 1
echo "X".TRUE."Y\n"; and then becomes a string.
FALSE is “not there” - it is
even “smaller” than zero, at
AB least when it comes to width.
X1Y
Equality versus Identity
<?php
$ans = 42;
if ( $ans == 42 ) {
print "Hello world!\n";
} else {
print "Wrong answer\n"; Hello World!
}
?>
Whitespace Does Not Matter
<?php
$ans = 42;
if ( $ans == 42 ) {
print "Hello world!\n";
} else {
print "Wrong answer\n";
}
?>
<?php
$ans = 42;
<?php if ( $ans == 42 )
$ans = 42; {
if ( $ans == 42 ) { print "Hello world!\n";
print "Hello world!\n"; }
} else { else
print "Wrong answer\n"; {
} print "Wrong answer\n";
?> }
?>
Aesthetics?
$x = 7; no
ye
if ( $x < 2 ) { s
x<10 print 'Medium'
print "Small\n";
} elseif ( $x < 10 ) { no
print "Medium\n";
} else { print 'LARGE'
print "LARGE\n";
}
• The break statement ends the current loop and jumps to the
statement immediately following the loop.
• It is like a loop test that can happen anywhere in the body of the loop.
Count: 1
for($count=1; $count<=10; $count++ ) { Count: 3
if ( ($count % 2) == 0 ) continue; Count: 5
echo "Count: $count\n"; Count: 7
} Count: 9
echo "Done\n"; Done
Function
• The real power of PHP comes from its functions.
• PHP has more than 1000 built-in functions, and in
addition you can create your own custom
functions.
User Defined Function
Besides the built-in PHP functions, it is possible to
create your own functions.
• A function is a block of statements that can be used repeatedly in a
program.
• A function will not execute automatically when a page loads or script
executed.
• A function will be executed by a call to the function.
Summary