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

More Related Content

What's hot

1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
jeeva indra
 
Advanced PHP: Design Patterns - Dennis-Jan Broerse
Advanced PHP: Design Patterns - Dennis-Jan BroerseAdvanced PHP: Design Patterns - Dennis-Jan Broerse
Advanced PHP: Design Patterns - Dennis-Jan Broerse
dpc
 

What's hot (19)

Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Php
PhpPhp
Php
 
PHP 5.3
PHP 5.3PHP 5.3
PHP 5.3
 
PHP Comprehensive Overview
PHP Comprehensive OverviewPHP Comprehensive Overview
PHP Comprehensive Overview
 
Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in India
 
Php notes 01
Php notes 01Php notes 01
Php notes 01
 
PHP
PHPPHP
PHP
 
PHP
PHPPHP
PHP
 
Component and Event-Driven Architectures in PHP
Component and Event-Driven Architectures in PHPComponent and Event-Driven Architectures in PHP
Component and Event-Driven Architectures in PHP
 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
 
Advanced PHP: Design Patterns - Dennis-Jan Broerse
Advanced PHP: Design Patterns - Dennis-Jan BroerseAdvanced PHP: Design Patterns - Dennis-Jan Broerse
Advanced PHP: Design Patterns - Dennis-Jan Broerse
 
Php Training
Php TrainingPhp Training
Php Training
 
Php
PhpPhp
Php
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
PHP slides
PHP slidesPHP slides
PHP slides
 
Was können wir von Rebol lernen?
Was können wir von Rebol lernen?Was können wir von Rebol lernen?
Was können wir von Rebol lernen?
 

Similar to John Rowley Notes

Similar to John Rowley Notes (20)

02 Php Vars Op Control Etc
02 Php Vars Op Control Etc02 Php Vars Op Control Etc
02 Php Vars Op Control Etc
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
 
Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
PHP MySQL
PHP MySQLPHP MySQL
PHP MySQL
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
Introduction To Lamp
Introduction To LampIntroduction To Lamp
Introduction To Lamp
 
PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
 
PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
 
Php
PhpPhp
Php
 
Babitha5.php
Babitha5.phpBabitha5.php
Babitha5.php
 
Babitha5.php
Babitha5.phpBabitha5.php
Babitha5.php
 
Php
PhpPhp
Php
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 

More from IBAT College

More from IBAT College (15)

Adrienne Gallagher Notes
Adrienne Gallagher NotesAdrienne Gallagher Notes
Adrienne Gallagher Notes
 
Helen Mc Glew Notes
Helen Mc Glew NotesHelen Mc Glew Notes
Helen Mc Glew Notes
 
James O Connor Notes
James O Connor NotesJames O Connor Notes
James O Connor Notes
 
John Murtagh Grad Dip Notes
John Murtagh Grad Dip NotesJohn Murtagh Grad Dip Notes
John Murtagh Grad Dip Notes
 
John Murtagh Hetac Notes
John Murtagh Hetac NotesJohn Murtagh Hetac Notes
John Murtagh Hetac Notes
 
John Murtagh Supervisory Mang Notes
John Murtagh Supervisory Mang NotesJohn Murtagh Supervisory Mang Notes
John Murtagh Supervisory Mang Notes
 
Keith Whitford Notes
Keith Whitford NotesKeith Whitford Notes
Keith Whitford Notes
 
Lisa Donaldson Notes
Lisa Donaldson NotesLisa Donaldson Notes
Lisa Donaldson Notes
 
Lorraine Ryan Notes
Lorraine Ryan NotesLorraine Ryan Notes
Lorraine Ryan Notes
 
Mark Dean Notes
Mark Dean NotesMark Dean Notes
Mark Dean Notes
 
Martin Quinn Notes
Martin Quinn NotesMartin Quinn Notes
Martin Quinn Notes
 
Morgan Campbell Notes Dtp
Morgan Campbell Notes DtpMorgan Campbell Notes Dtp
Morgan Campbell Notes Dtp
 
Olivia Edge Hr Notes
Olivia Edge Hr NotesOlivia Edge Hr Notes
Olivia Edge Hr Notes
 
Paul Lydon Notes Acca F9
Paul Lydon Notes Acca F9Paul Lydon Notes Acca F9
Paul Lydon Notes Acca F9
 
Morgan Campbell Advanced Dtp Notes
Morgan Campbell Advanced Dtp NotesMorgan Campbell Advanced Dtp Notes
Morgan Campbell Advanced Dtp Notes
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

John Rowley Notes

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