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 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 ScriptNote 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.
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 VariablesYou 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 commentsEspecially when you are still new to the language it is good to put comments around your codeThree ways to include a PHP commentAnything typed to the right of two forward slashes // is a comment as in // this is a commentAnything 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 formPHP 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 Let us put it together using forms pex2.html & pex2.php <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";?>