110 likes | 264 Vues
This lesson provides an overview of PHP, one of the most popular web development languages worldwide. It covers topics such as PHP’s role as an interpreted language, the importance of coding standards, and the embedding of PHP within HTML. You’ll also learn about PHP opening and closing tags, and how to properly comment your code for readability. Sample PHP programs are included to illustrate the concepts. Lastly, students are tasked with fixing errors in a provided PHP script to reinforce their understanding.
E N D
Introduction to PHP Lesson 3
PHP is the most popular web development language in the world • PHP sources of Information • http://php.net • http://www.zend.com • http://www.hostscripts.com • PHP: Hypertext Preprocessor • PHP dealt with before HTML • Lies between web server and HTML • Interpreted language (i.e. not compiled) • Code is interpreted on the fly • Code is in plain text, so anyone with access to the web serves can view it • However, code can still be encrypted so it can’t be stolen. Sites that offer facilities for encryption include http://www.zend.com and IOnQ
PHP Programming standards • Goal is to make code easy to read and understand • However, there is NO accepted industry coding standards • Each programmer is encouraged to come up with his own standard or if he’s a part of a company and the company has its own standard then he is required to follow that standard.
Use Comments • Use comments but don’t comment everything because that would required a huge amount of time. // # /* .. */ Single line comment Multi- Line comment • Better if just make the code readable and simple • Also, use indentation
Embed PHP within HTML • If you need to create a page with few dynamic elements • Embed HTML with PHP • If you need to create a long complex PHP script
PHP Opening and Closing Tags • <?php….?> - this setting is always acceptable in any PHP configuration • <? …?> - this short_tag setting may not be available depending on your PHP config file (php.ini); make sure that in php.ini there is a line short_tag= on • <script language=“php”>…</script> - html style tag • <% … %> - ASP style tag
NOTE: • Each HTML browser may interpret some HTML tags in a different way. By embedding HTML within PHP you are assured that HTML will be interpreted in only 1 way. • However, if there is a need to change code and the programmer who will do it is not familiar with PHP then that would be a problem. So separate those parts that can be separated from PHP. • Ex. <html><head> tags…</body></html> • Note also that every PHP statement ends in a semi-colon (;)
Sample1 PHP program <html><body>// save as hello.php in your htdocs folder <?phpecho "Hello World!"; //you can also use print ?></body></html>
Sample2 PHP program <html><body>//save this as info.php in your htdocs folder<?phpphpinfo();?></body></html>
phpinfo() • is a php function that outputs information about your PHP settings: • Tells you the PHP version installed • Status of PHP Core variables • Note that there are local and Mastery Values. • The local value can be set on a page-by-page basis, by .htaccess file, or by a domain configuration. • The master value is the global setting as specified in the php.ini (of which only the System Administrators have access)
PHP Exercise 1: • Fix the errors in the PHP script (erroneous.php) open the link. Save you solution as my_solution_SURNAME.php erroneous.php