Intro To PHP
Intro To PHP
http://localhost/COMP1841/week1/hello.php
View Source hello.php
Hello Bold
http://localhost/COMP1841/week1/hello_bold.php
View Source hello_bold.php
Some Syntax Considerations
• PHP is not case sensitive.
• Hence ECHO is the same as echo
• But pick a system and stick to it, I go
with lower case.
• PHP sees any code after // on a line or
between /* and */ as a comment.
comments
Reserved Words
PHP contains reserved words like JavaScript and most
other languages and these can not be used as
variable names. Some examples:
• break
• case
• continue
• do
• echo()
• empty()
• if … else … elseif … endif
Variables & Data types
• All variables in PHP start with a $(dollar) sign. For example:
http://localhost/COMP1841/week1/DataTypes.php
Print and Echo
• These built in functions are used to send data to
the browser, or write to the screen.
• echo( )
• print( )
• Both work the same, I use echo( ) the most, you
may see print( ) in other examples
• You can use single quotes or double quotes in or
out of brackets.
• Begin by using echo and double quotes
Print and echo cont…
Single or double quotes
Values in single quotes are treated literally, those in
double quotes are interpreted, this is called variable
interpolation. Consider this code:
http://localhost/COMP1841/week1/escape_error.php
PHP Functions
http://localhost/COMP1841/week1/function.php
Multiple Arguments
http://localhost/COMP1841/week1/function_multi.php
Arithmetical Operators
String concatenation
http://localhost/COMP1841/week1/concat.php
Logical Operators in PHP
Assignment Operators
Comparison Operators
Date and Time Functions
• PHP provides a predefined Date and Time function that
can be used to time stamp events or display relevant
messages to users (e.g. based on time or day of the week)
• Date (format string)
• key format strings :
• D - textual representation of a day, three letters i.e. Mon to Sun;
• F - for full months of the year from January to December and
• H - full 24 hours formatted clock – please note that there is
difference between H and h string or D and d string etc. )
• More at http://www.w3schools.com/php/php_date.asp
Date and Time example
http://localhost/COMP1841/week1/date.php
Dynamic copyright
http://localhost/COMP1841/week1/copy.php
Control Structures
Conditional statements or if statements
Conditional statements or if statements
https://www.w3schools.com/php/php_if_else.asp
Loops - The for loop – when we know up
front how many times we need to run the same code
For Loop
https://www.w3schools.com/php/php_looping_for.asp
The while loop - Loops through a block of code as long as
the specified condition is true. Like repeating if statements.
While Loop
https://www.w3schools.com/php/php_looping_while.asp
Foreach Loop
https://www.w3schools.com/php/php_looping_foreach.asp
Any Questions