PHP Hypertext Preprocessor What is PHP for t

  • Slides: 12
Download presentation
PHP : Hypertext Preprocessor

PHP : Hypertext Preprocessor

What is PHP for? t Unlike Java. Script which is a client-side script language,

What is PHP for? t Unlike Java. Script which is a client-side script language, PHP is a server side script t 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. t PHP programs/scripts perform several basic operations n Obtain data from a user n Perform computations n Access and manipulate data stored in files and databases n Display data so that a user can view it.

My first PHP script t Inside the regular html file t You include the

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

My first PHP script: pex 1. php <? php echo 'Hello World'; echo '

My first PHP script: pex 1. php <? php echo 'Hello World'; echo ' <h 1> Hello World</h 1>'; 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 t Note that inside the echo command, it is similar to writing

PHP Script t Note that inside the echo command, it is similar to writing code on regular HTML. t Remember that the PHP script will be preprocessed. t Therefore when you try and view the code, it does not have the php scripts but rather the final processed text. t Therefore, you have to upload it to the server to see how it looks like. If you preview, you will see garbage.

Variables t Similar to Java. Script, you can work with variables. t Variables must

Variables t Similar to Java. Script, you can work with variables. t Variables must begin with a dollar sign ($). t You can generally name a variable anything you like as long as it begins with a $ and a letter or a underscore “_”. t For example, a good variable name might be $first. Name or $first_name. t DO NOT include blank spaces in a variable name. t 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 t A variable can store integers as in $num = 5; t or

Variables t A variable can store integers as in $num = 5; t or it can store a decimal number as in n $price = 19. 99; t or it can store a string (i. e. a word or phrase) as in n $name = 'John Doe'; t Variables in php do not need to be declared with specific data types n

Variables t You can use the dot operator (. ) to concatenate two strings

Variables t 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 $whole. Name. n n n $first. Name = 'John'; $last. Name = 'Doe'; $whole. Name = $first. Name. ' '. $last. Name;

Using comments t Especially when you are still new to the language it is

Using comments t Especially when you are still new to the language it is good to put comments around your code t Three ways to include a PHP comment n n n 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” t PHP to access data submitted by a form

Process data from a “form” t PHP to access data submitted by a form t PHP creates an array using data sent using a POST action request. t To assess variables in a POST request, you can use the $_POST array. n e. g. <input type="text" name="val 1" value=""> t $_POST['val 1'] will have the value of what you store in the textbox above.

Let us put it together using forms pex 2. html & pex 2. php

Let us put it together using forms pex 2. html & pex 2. php pex 2. html <html> <head> </head> <body> <form action= "pex 2. php" method="post"> Enter your message: <input type="text" name="msg" size="30"> <input type="submit" value="Send"> </form></body> </html> pex 2. php <? php $input=$_POST['msg']; echo "You said: $input"; ? >

pex 3. html, pex 3. php Error Message 1 Error Message 2 Correct Response

pex 3. html, pex 3. php Error Message 1 Error Message 2 Correct Response t Create a form that allows you to enter your first and last name. t Check that both names are not empty using PHP using strlen(). t The command trim(variable_name) trims any spaces in the beginning or end and returns the new string. t The command strlen(variable_name) counts the number of characters in the variable_name. t So you can combine strlen(trim(variable_name) to count the number of characters omitting any spaces in front or back. t The if (condition) works the same as Java. Script: eg. if ($a == $b) { echo (‘<b>HELP</b>’); }