110 likes | 496 Views
Web Development. PHP Consolidation. Content. This week we are going to go over a number of important areas You need to know these well as when you come to add SQL to your php , it starts to look complicated if you don’t know the basics. Includes.
E N D
Web Development PHP Consolidation
Content • This week we are going to go over a number of important areas • You need to know these well as when you come to add SQL to your php, it starts to look complicated if you don’t know the basics
Includes • Using the PHP includes function, you can add standard pieces of code • The parser will add the code in the included file as if you had typed the code there yourself • Use the include function inside php contained with the <body> of the page • Note that the php file that is included does not need <html> tags. Why not? • Try out the include demo php file now
Includes • We would use this: • to avoid retyping code that needs repeating between several pages • because changing the included file once is easier than changing multiple instances of the same code in many files
Functions • These are used for similar reasons as for includes – they avoid code duplication • Code duplication is bad (especially from a code maintenance viewpoint) • The can be placed anywhere and called from anywhere in the PHP code • You can pass several values to them, and can pass one value back if you want • Try out the 5 function demo programs
Global variables • Global variables could be used to return several values from a function • Note: the use of global variables should be limited because they are viewed as bad programming practice • We should limit the scope of our variables as much as possible • NOTE: PHP has many built in functions – don’t reinvent the wheel!
Arrays • Arrays are implemented much like any other programming language • They are values mapped by keys stored under a single variable name • In PHP, the keys can be numeric or alphanumeric • PHP has lots of functions to sort arrays • Arrays can be multidimensional • Try out the array test program
Foreach • Often we need to step through an array and do something with each element contained therein • using normal loops, we need to know what index the array starts with and how big it is • Also PHP arrays do not need to use numeric keys! • A simple answer is provided – use foreach • Try out the foreach test program
heredoc • The process of outputting HTML using the echo command is cumbersome because of the problem of using quotation marks • You may like to use heredoc instead • start the heredoc like this EOD<<< • end with this EOD; • You don’t have to use EOD – anything will do • Try out the example programs, heredoc and heredoc1
heredoc • Warning: heredoc is fussy! • Don’t put anything after the <<< on the opening line or after the ; on the closing line • Also, there must be no leading characters whatsoever, not even spaces or tabs on the closing and opening heredoc lines • You can waste a lot of time trying to fix this very simple error!!
print_r • When dealing with arrays, it can be useful to know what values they contain • Use the print_r command to print on the screen what the array contains • This will prove useful when you dot he assignment! • try out the print_r example program