SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

PHP Comprehensive Overview
PHP Comprehensive OverviewPHP Comprehensive Overview
PHP Comprehensive OverviewMohamed Loey
 
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 IndiaDeepak Rajput
 
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 PHPStephan Schmidt
 
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_masterjeeva 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 Broersedpc
 
Php Training
Php TrainingPhp Training
Php Trainingadfa
 
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?lichtkind
 

La actualidad más candente (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 a John Rowley Notes

Similar a 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
 

Más de IBAT College

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

Más de 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
 

Último

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Último (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.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