160 likes | 387 Views
PHP : Hypertext Preprocessor. What is PHP for?. Unlike JavaScript which is a client-side script language, PHP is a server side script PHP is processed on the server, therefore you can not use the View/Source menu command to view someone else's PHP code. Finally someone is protecting my code.
E N D
What is PHP for? • Unlike JavaScript which is a client-side script language, PHP is a server side script • PHP is processed on the server, therefore you can not use the View/Source menu command to view someone else's PHP code. Finally someone is protecting my code. • PHP programs/scripts perform several basic operations • Obtain data from a user • Perform computations • Access and manipulate data stored in files and databases • Display data so that a user can view it.
My first PHP script • Inside the regular html file • You include the following tag <?php • // code here ?> • Make sure you save it as .php
My first PHP script: pex1.php <?php echo 'Hello World'; echo '<br /> <h1> Hello World</h1>'; echo '<em> Hello World </em> '; ?> ! Before you upload to your server, make sure you make a directory call php so we know where to look for your code.
PHP Script • Note that inside the echo command, it is similar to writing code on regular HTML. • Remember that the PHP script will be pre-processed. • Therefore when you try and view the code, it does not have the php scripts but rather the final processed text. • Therefore, you have to upload it to the server to see how it looks like. If you preview, you will see garbage.
Variables • Similar to JavaScript, you can work with variables. • Variables must begin with a dollar sign ($). • You can generally name a variable anything you like as long as it begins with a $ and a letter or a underscore “_”. • For example, a good variable name might be $firstName or $first_name. • DO NOT include blank spaces in a variable name. • If you choose to make a variable name out of two or more words, I recommend using an underscore ( _ ) between the two words or capitalizing the second word so the variable name is easier to read.
Variables • A variable can store integers as in • $num = 5; • or it can store a decimal number as in • $price = 19.99; • or it can store a string (i.e. a word or phrase) as in • $name = 'John Doe'; • Variables in php do not need to be declared with specific data types
Variables • You can use the dot operator ( . ) to concatenate two strings together into one string. Remember to add a blank space if necessary with ' ' or " ". For example the following code segment will result in the name "John Doe" being stored in the variable $wholeName. • $firstName = 'John'; • $lastName = 'Doe'; • $wholeName = $firstName . ' ' . $lastName;
Using comments • Especially when you are still new to the language it is good to put comments around your code • Three ways to include a PHP comment • Anything typed to the right of two forward slashes // is a comment as in // this is a comment • Anything enclosed within the symbols /* and */ is a comment. This kind of comment can extend to multiple lines of code /* this is a comment this is still a comment this is a comment */ • Anything typed to the right of a pound symbol ( # ) is a comment # this is a comment
Process data from a “form” • PHP to access data submitted by a form • PHP creates an array using data sent using a POST action request. • To assess variables in a POST request, you can use the $_POST array. • e.g. <input type="text" name="val1" value=""> • $_POST['val1'] will have the value of what you store in the textbox above.
pex2.html <html> <head> </head> <body> <form action= "pex2.php" method="post"> Enter your message: <input type="text" name="msg" size="30"> <input type="submit" value="Send"> </form></body> </html> pex2.php <?php $input=$_POST['msg']; echo "You said: $input"; ?> Let us put it together using formspex2.html & pex2.php
pex3.html, pex3.php • Create a form that allows you to enter your first and last name. • Check that both names are not empty using PHP using strlen(). • The command trim(variable_name) trims any spaces in the beginning or end and returns the new string. • The command strlen(variable_name) counts the number of characters in the variable_name. • So you can combine strlen(trim(variable_name) to count the number of characters omitting any spaces in front or back. • The if (condition) works the same as JavaScript: eg. if ($a == $b) { echo (‘<b>HELP</b>’); } Error Message 1 Error Message 2 Correct Response