170 likes | 365 Views
Web Development. PHP intro. What is it ?. PHP is a server side scripting language It is also a pre-processor as it processes web pages before sending them to the client There are other server side languages out there, but php is open source and therefore free!
E N D
Web Development PHP intro
What is it? • PHP is a server side scripting language • It is also a pre-processor as it processes web pages before sending them to the client • There are other server side languages out there, but php is open source and therefore free! • Unicentre (actually Moodle) is written in php
How to get it • Download and set up a portable version of EasyPHP on your USB drive so that you can take the server around with you and use it on any PC • A portable version of an application doesn’t amend the Windows’ registry (and so won’t appear in the Start menu)
How HTML works user types in the URL (.htm) Request sent to server, HTML page returned HTML page parsed by the browser and displayed
How php works user types in the URL (.php) Request sent to server, php processed and HTML page returned HTML page parsed by the browser and displayed
How does it fit in with HTML? • php script is embedded within the HTML • because of this, all php is written inside tags • the php tags are <?php ……… ?> • php uses syntax that is very similar to C (and therefore also like Java) • for example, all php lines end in a semicolon ; • unlike HTML, it is a proper programming language that supports sequence, selection and repetition
First php program <html> <head> <title>My first php program</title> <body> <?php echo “hello world”; ?> </body> </html>
Notes • In html written in PowerPoint I omit the Doctype required by xHTML. • This is purely to keep the code manageable on screen • In the real world, you should use the doctype • echo prints to the html document that will be sent to the browser for parsing, effectively printing it on the screen as it is outside of any tag
Coding Conventions • You should observe normal coding conventions, namely: • indent your code to make it readable • use comments (// or /*…..*/) • use meaningful variable names • Programming is usually a team pursuit and you will make yourself very unpopular if you write code that is illegible
HTML with php <html> <head> <title>My first php program</title> <body> <?php echo “<h1>hello world</h1>”; ?> </body> </html>
Integration Important note: • the phpgoes in the html, not the other way around • remember, php is written inside HTML tags! • the code on the following slide will work, but is incorrect and must be avoided:
Integration <?php echo “<html>”; echo “<head>”; echo “<title>My first php program</title>”; echo “<body>”; echo “<h1>hello world</h1>”; echo “</body>”; echo “</html>”; ?>
Variables • php is a loosely typed language • this means that you do not have to declare variables before using them • neither do you have to declare what type of data you want to store there – this will be sorted out automatically for you • all variables must start with a dollar sign $
Variables • e.g. <?php $txt="Hello World!"; $x=16;?>
Similarities to other languages • php has many built in functions • it will save you a lot of time if you get to know these and get used to looking to see if there is a function to do what you want to do before you write some code to do it! • There’s no point reinventing the wheel!
Similarities to other languages • the loop functions (for, while, do…while) are the same as for C • the selection statements are the same as for C (if…else and case… switch) • arrays are very similar too