1 / 12

PHP : Hypertext Preprocessor

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. 

Download Presentation

PHP : Hypertext Preprocessor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP : Hypertext Preprocessor

  2. 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.

  3. My first PHP script • Inside the regular html file • You include the following tag <?php • // code here ?> • Make sure you save it as .php

  4. 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.

  5. 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.

  6. 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.

  7. 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

  8. 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;

  9. 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

  10. 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.

  11. 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

  12. 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

More Related