PHP and MySQL Course<br />John Rowley<br />1<br />
Part 2: Getting Started with PHP<br />Hello World<br />Syntax Rules<br />Escaping Characters<br />Reserved Words<br />Vari...
Exercise 2.1 – Hello World<br />Version 1<br />&lt;?php<br />echo “Hello World”;<br />?&gt;<br />Version 2<br />&lt;html&g...
Syntax Rules - Comments<br />PHP code is generally case insensitive<br />Use of semi-colon at the end of statements<br />/...
Escaping Characters<br />escape1.php<br />The ‘’ backslash character can be used to escape special characters<br />Commonl...
Variables<br />A variable is a place in which to store data for manipulation within a script.<br />All php variables begin...
Data Types<br />String – strings of spaces, text, numeric characters, specified within double quotes (“...”) or single quo...
Data Types<br />&lt;?php<br />	$str = &quot;Here is a string&quot;;<br />	$int = 77;<br />	$flt = 3.142;<br />	$non = NULL...
Functions<br />A function is a piece of code that can be executed once or many types by the script. Functions and variable...
Functions<br />&lt;html&gt;  &lt;head&gt;   &lt;title&gt;PHP Functions&lt;/title&gt;  &lt;/head&gt;<br /> &lt;body&gt;<br ...
Function Arguments<br />The plain brackets that follow function names can be used to provide data for use in the code that...
Function Arguments<br />&lt;html&gt; &lt;head&gt;  &lt;title&gt;PHP Arguments&lt;/title&gt;<br /> &lt;/head&gt; &lt;body&g...
Multiple Functions<br />PHP functions can call other functions during the script processing, just like the echo statement....
Variable scope<br />Scope defines which parts of a PHP script have access to a variable. Variables declared inside a funct...
Variable scope<br />Scope defines which parts of a PHP script have access to a variable. Variables declared inside a funct...
Variable scope<br />&lt;?php <br />	$num=0;<br />	function make_triple($arg)<br />	{<br />	  global $num;<br />	  $num = $...
Multiple Arguments<br />Functions make specify multiple arguments within their plain brackets to allow several values to b...
Multiple Arguments<br />&lt;?php <br />	function addup( $a = 32, $b = 32, $c = 32)<br />	{<br />	  $total = $a + $b + $c;<...
Exercise<br />Write a series of functions to generate a table  and a number of rows and cells. The cell function should ta...
Upcoming SlideShare
Loading in …5
×

John Rowley Notes

581 views

Published on

Lecturer: John Rowley

PHP MySQL notes

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
581
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

John Rowley Notes

  1. 1. PHP and MySQL Course<br />John Rowley<br />1<br />
  2. 2. Part 2: Getting Started with PHP<br />Hello World<br />Syntax Rules<br />Escaping Characters<br />Reserved Words<br />Variables<br />Data Types<br />Functions<br />Function Arguments<br />Multiple Functions<br />Variable Scope<br />Multiple Arguments<br />2<br />
  3. 3. Exercise 2.1 – Hello World<br />Version 1<br />&lt;?php<br />echo “Hello World”;<br />?&gt;<br />Version 2<br />&lt;html&gt;<br />&lt;?php<br /> echo “&lt;h1&gt;Hello World&lt;/h1&gt;”;<br />?&gt;<br />&lt;/html&gt;<br />Notes:<br />Use of &lt;?php opening tag instead of &lt;?<br />; // semi-colon at end of statements<br />Mixing of html and php code generally frowned upon in professional programming but ok for small projects<br />helloworld.php<br />3<br />
  4. 4. Syntax Rules - Comments<br />PHP code is generally case insensitive<br />Use of semi-colon at the end of statements<br />// used for single line comments<br /># used for single line comments<br />/* ..... */ used for multi-line comments<br />Notice HTML tags embedded in .php file<br />Example<br />&lt;html&gt;<br />&lt;?php<br /> // Single Line Comment<br /> # <br /> echo “&lt;h1&gt;Hello World&lt;/h1&gt;”;<br />?&gt;<br />&lt;/html&gt;<br />helloworld2.php<br />4<br />
  5. 5. Escaping Characters<br />escape1.php<br />The ‘’ backslash character can be used to escape special characters<br />Commonly used when you want to use quotation marks within a text string being printed out with the echo statement (“) within quotes<br />‘ ’ used for newline<br />‘ ’ used for tabs<br />Note – if you are outputting a web page the above can be used to format the souce code – you need to use &lt;p&gt; and &lt;br&gt; tags to format text.<br />&lt;?php<br />echo “&lt;p&gt;&lt;form&gt;”;<br />echo “ &lt;textarea rows=”5” cols=”48”&gt;”;<br />echo “Demonstration Text&lt;br /&gt;”;<br />echo “&lt;/textarea&gt;”;echo “&lt;/form&gt;&lt;/p&gt;”;<br />?&gt;<br />5<br />
  6. 6. Variables<br />A variable is a place in which to store data for manipulation within a script.<br />All php variables begin with $ character.<br />Variables can contain _, letters and digits but cannot start with a number<br />$myFirstvar<br />$_demo<br />$var345<br />Data is assigned using the = operator<br />&lt;?php<br />$formText = &quot;&lt;form&gt; &lt;p&gt; &lt;textarea rows=”5” cols=”48”&gt; Demonstration Text goes here&lt;/textarea&gt; &lt;/form&gt;&quot;;<br />echo $formText;<br />?&gt;<br />variable1.php<br />6<br />
  7. 7. Data Types<br />String – strings of spaces, text, numeric characters, specified within double quotes (“...”) or single quotes (‘...’);<br />Integer – numbers without decimal places, like 1000<br />Floating-point – numbers with decimal places, like 3.142<br />Boolean – a truth value which can be either TRUE or FALSE (also true, false)<br />NULL – no value at all<br />7<br />
  8. 8. Data Types<br />&lt;?php<br /> $str = &quot;Here is a string&quot;;<br /> $int = 77;<br /> $flt = 3.142;<br /> $non = NULL;<br /> echo(&quot;String:$str&lt;br&gt;&quot;);<br /> echo(&quot;Integer:$int&lt;br&gt;&quot;);<br /> echo(&quot;Floating-point:$flt&lt;br&gt;&quot;);<br /> echo(&quot;Null:$non&lt;br&gt;&quot;);<br />?&gt;<br />datatypes.php<br />8<br />
  9. 9. Functions<br />A function is a piece of code that can be executed once or many types by the script. Functions and variables form the heart of PHP programming<br />PHP has lots of functions for manipulating strings, dates, maths, databases etc. but you will want to create your own as well<br />Example<br />function printName() {<br />echo “My name is Jack”;<br />} <br />9<br />
  10. 10. Functions<br />&lt;html&gt; &lt;head&gt; &lt;title&gt;PHP Functions&lt;/title&gt; &lt;/head&gt;<br /> &lt;body&gt;<br /> &lt;?php <br /> /*<br /> A function starts with the keyword function<br /> followed by the name of the function<br /> following by opening bracket<br /> and a closing bracket <br /> then an opening brace<br /> The content of the function and then<br /> the closing brace<br /> */<br /> function go(){ echo(&quot;PHP adds dynamic content&lt;hr&gt;&quot;); } <br /> ?&gt;<br /> &lt;?php go(); ?&gt;<br /> &lt;p&gt;*** HTML is great for static content ***&lt;/p&gt;<br /> &lt;?php go(); ?&gt;<br /> &lt;/body&gt;&lt;/html&gt;<br />functions1.php<br />
  11. 11. Function Arguments<br />The plain brackets that follow function names can be used to provide data for use in the code that can be executed by that function. <br />This data is known as an ‘argument’ and a function can take several ‘arguments’<br />Example<br />function printName($name) {<br />echo “My name is $name”;<br />}<br />11<br />
  12. 12. Function Arguments<br />&lt;html&gt; &lt;head&gt; &lt;title&gt;PHP Arguments&lt;/title&gt;<br /> &lt;/head&gt; &lt;body&gt;<br /> &lt;?php <br /> function go($arg)<br /> { <br /> echo(&quot;&lt;b&gt;&lt;u&gt;&lt;i&gt;$arg&lt;/i&gt;&lt;/u&gt;&lt;/b&gt;&quot;); <br /> } <br /> ?&gt;<br /> &lt;p&gt;This is the regular text style of this page.&lt;/p&gt;<br /> &lt;?php go(&quot;This text has added style&quot;); ?&gt;<br /> &lt;p&gt;This is the regular text style of this page.&lt;p&gt;<br /> &lt;?php go(&quot;PHP makes this so easy&quot;); ?&gt;<br /> &lt;/body&gt;&lt;/html&gt;<br />functions2.php<br />12<br />
  13. 13. Multiple Functions<br />PHP functions can call other functions during the script processing, just like the echo statement.<br />&lt;?php <br /> function show_number($num)<br /> {<br /> $new_number = make_double($num);<br /> echo(&quot;The value is $new_number&quot;);<br /> }<br /> function make_double($arg)<br /> {<br /> return $arg + $arg;<br /> } <br />?&gt;<br />&lt;html&gt; &lt;head&gt; &lt;title&gt;PHP Functions&lt;/title&gt; &lt;/head&gt;<br /> &lt;body&gt;<br /> &lt;h3&gt; &lt;?php show_number(4); ?&gt; &lt;/h3&gt;<br /> &lt;/body&gt;&lt;/html&gt;<br />functions3.php<br />13<br />
  14. 14. Variable scope<br />Scope defines which parts of a PHP script have access to a variable. Variables declared inside a function are known as ‘local’ variables and can only be used in the function within which it is declared.<br />If you want to use a variable between different functions, one way is to declare it with the keyword ‘global’ within the function to access it and change it<br />Good programming practice to declare and initialise it it outside the function first<br />14<br />
  15. 15. Variable scope<br />Scope defines which parts of a PHP script have access to a variable. Variables declared inside a function are known as ‘local’ variables and can only be used in the function within which it is declared.<br />If you want to use a variable between different functions, one way is to declare it with the keyword ‘global’ within the function to access it and change it<br />Good programming practice to declare and initialise it it outside the function first<br />15<br />
  16. 16. Variable scope<br />&lt;?php <br /> $num=0;<br /> function make_triple($arg)<br /> {<br /> global $num;<br /> $num = $arg + $arg +$arg;<br /> thrice();<br /> }<br /> function thrice()<br /> {<br /> global $num;<br /> echo(&quot;The value is $num&quot;);<br /> } <br />?&gt;<br />&lt;html&gt; &lt;head&gt; &lt;title&gt;Variable Scope&lt;/title&gt; &lt;/head&gt;<br /> &lt;body&gt;<br /> &lt;h3&gt; &lt;?php make_triple(4); ?&gt; &lt;/h3&gt; &lt;/body&gt;&lt;/html&gt;<br />scope1.php<br />16<br />
  17. 17. Multiple Arguments<br />Functions make specify multiple arguments within their plain brackets to allow several values to be passed to the function code. The argument variable names are separated by commas in a list<br />When you specify multiple arguments to a function, all those arguments must be passed to the funcition<br />However, you can supply default values for the arguments in the declaration so the default value is used if you do not pass it.<br />17<br />
  18. 18. Multiple Arguments<br />&lt;?php <br /> function addup( $a = 32, $b = 32, $c = 32)<br /> {<br /> $total = $a + $b + $c;<br /> echo(&quot;$a + $b + $c = $total&quot;);<br /> }<br />?&gt;<br />&lt;html&gt; &lt;head&gt; <br />&lt;title&gt;Function Arguments&lt;/title&gt; &lt;/head&gt;<br /> &lt;body&gt;<br /> &lt;h3&gt; &lt;?php addup(8, 16, 24); ?&gt; &lt;/h3&gt;<br /> &lt;h3&gt; &lt;?php addup(8, 16); ?&gt; &lt;/h3&gt;<br /> &lt;/body&gt;<br />&lt;/html&gt;<br />functions4.php<br />18<br />
  19. 19. Exercise<br />Write a series of functions to generate a table and a number of rows and cells. The cell function should take an argument which specifies what should be output in the cell.<br />Then call the functions in order so that the table is generated.<br />These functions are useful when writing programs as they help keep the html separate from the coding so you can see what is going on in your code, and easy to change if you have modify css styles etc. You just change them them within the function.<br />Example<br />output_table_header();<br />output_start_row();<br />output_cell(“Test”);<br />output_cell(“Testing”);<br />output_cell(“Final Test”);<br />output_end_row();<br />output_table_footer();<br />solution_exercise1.php<br />19<br />

×