Introduction To: Tutorial For Beginners
Introduction To: Tutorial For Beginners
www.genial-code.com
Science
ScienceCalculations
Calculations
System
System
System
System
CCuses
usescurly
curly
braces
braces {}}for
{ for
code
codeblocks.
blocks.
Scripting/
Scripting/
Interpreted
Interpreted
About the PHP Language
• Syntax inspired by C
- Curly braces, semicolons, no signficant whitespace
• Syntax inspired by perl
- Dollar signs to start variable names, associative arrays
• Extends HTML to add segments of PHP within an HTML file
Philosophy of PHP
• You are a responsible and intelligent programmer.
• You know what you want to do.
• Some flexibility in syntax is OK - style choices are OK.
• Let’s make this as convenient as possible.
• Sometimes errors fail silently.
<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>
<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
• You can run PHP from the <?php
echo("Hello World!");
command line - the output
echo("\n");
simply comes out on the ?>
terminal.
• It does not have to be part of
a request-response cycle.
Basic Syntax
Keywords
Abstract and array() as break case catch class clone
const continue declare default do else elseif end
declare endfor endforeach endif endswitch endwhile
extends final for foreach function global goto if
implements interface instanceof namespace new or
private protected public static switch $this throw try
use var while xor
Variable Names
• Start with a dollar sign ($) followed by a letter or underscore,
followed by any number of letters, numbers, or underscores
• Case matters
http://php.net/manual/en/language.variables.basics.php
Variable Name Weirdness
Things that look like variables but are missing a dollar sign can be
confusing.
$x = 2; $x = 2;
$y = x + 5; y = $x + 5;
print $y; print $x;
5 Parse error
Variable Name Weirdness
Things that look like variables but are missing a dollar sign as an
array index are unpredictable....
$x = 5;
$y = array("x" => "Hello");
print $y[x];
Hello
Strings / Different + Awesome
• String literals can use single quotes or double quotes.
• The backslash (\) is used as an “escape” character.
• Strings can span multiple lines - the newline is part of the
string.
• In double-quoted strings, variable values are expanded.
• Concatenation is the "." not "+" (more later).
http://php.net/manual/en/language.types.string.php
<?php Double Quote
echo "this is a simple string\n";
echo "You can also have embedded newlines in
strings this way as it is
okay to do";
// Outputs: This will expand:
// a newline
echo "This will expand: \na newline";
// Outputs: Variables do 12
$expand = 12;
echo "Variables do $expand\n";
<?php
echo 'this is a simple string';
Single Quote
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
Comments in PHP
echo 'This is a test'; // This is a c++ style comment
/* This is a multi line comment
yet another line of comment */
echo 'This is yet another test';
echo 'One Final Test'; # This is a shell-style comment
http://php.net/manual/en/language.basic-syntax.comments.php
Output
<?php
• echo is a language construct - can $x = "15" + 27;
be treated like a function with one echo $x;
parameter. Without parentheses, echo("\n");
it accepts multiple parameters. echo $x, "\n";
print $x;
• print is a function - only one print "\n";
print($x);
parameter, but parentheses are
print("\n");
optional so it can look like a
?>
language construct.
Expressions
Expressions
• Completely normal like other languages ( + - / * )
• More agressive implicit type conversion
<?php
$x = "15" + 27;
echo($x); 42
echo("\n");
?>
Expressions
• Expressions evaluate to a value. The value can be a string,
number, boolean, etc.
• Expressions often use operations and function calls. There is
an order of evaluation when there is more than one operator in
an expression.
• Expressions can also produce objects like arrays.
Operators of Note
• Increment / Decrement ( ++ -- )
• String concatenation ( . )
• Equality ( == != )
• Ternary ( ? : )
$x = 12;
$y = 15 + $x++; x is 13 and y is 27
echo "x is $x and y is $y \n";
Increment / Decrement
• These operators allow you to both retrieve and increment /
decrement a variable.
• They are generally avoided in civilized code.
$x = 12;
$y = 15 + $x; x is 13 and y is 27
$x = $x + 1;
echo "x is $x and y is $y \n";
String Concatenation
PHP uses the period character for concatenation, because the plus
character would instruct PHP to do the best it could to add the two
things together, converting if necessary.
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 and then
echo "X".TRUE."Y\n"; becomes a string. FALSE is “not
there” - it is even “smaller” than zero,
at least when it comes to width.
AB
X1Y
Equality versus Identity
The equality operator (==) in PHP is far more agressive than in
most other languages when it comes to data conversion during
expression evaluation.
<?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";
}
?>
$x = 7; no
yes
if ( $x < 2 ) {
x<10 print 'Medium'
print "Small\n";
} elseif ( $x < 10 ) { no
print "Medium\n";
} else { print 'LARGE'
print "LARGE\n";
}
• 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
Summary
This is a sprint through some of the unique
language features of PHP.