Learning
php
 
What Php
can do

 

  • server-side scripting
  • command-line scripting
  • desk-top applications
  • interfacing with a database (most are supported in PhP)
  • interfacing with LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM
  • Outputting images, pdf files, etc.

PHP Documentation

basic syntax PHP used for server-side scripting is typically embedded in XHTML code but the file is named with a php extension.

The extension php on the filename signals that the PHP interpreter should be executed to process the file before it is sent to the end-user.

The interpreter only executes the statements between the beginning php tag <?php and the ending php tag ?>.  The rest of the text(XHTML) in the file is simply written as-is to the file that will be sent to the end-user.

PHP is case-sensitive.

PHP statements end with a semi-colon ;

PHP uses the curly braces { } to block statements.

Basic embedded php syntax:

<?php
    .... php statements to execute
?>

 

Creating your first
php script

Try This:

Create the following php script in notepad:

<html>
<head>
<title>PHP Test</title>
</head>
<body>


<?php
print ("Hello world\n");
?>


</body>
</html>

Save the file as php_ex1.php

Upload it to Linux

Open the file using Internet Explorer

View source

Edit the file -> change the print statement to

        print ("<br><br> <h1>Hello <br>world</h1><br>\n");

Save the file as php_ex2.php

Upload it to Linux

Open the file using Internet Explorer

View source

variables
Data types:

Four scalar types:

Two compound types:

Variables are typically not typed but the data type is determined at runtime.

Variable names:

start with a dollar sign, followed by a valid PHP identifier.
The PHP identifier could be a field name from a form or a field of a table in a database table.

PHP identifier:

letter or underscore, followed by alphanumeric or underscore (cannot match a PHP keyword).

* Always a good idea to choose an identifier that is meaningful!

Values sent from a form:

Must be captured into PHP variables using the form field name in a $_REQUEST statement:

Example:


Part of a form:
<form action="process_form.php" method=post>

Enter your name:<input type="text" name="username">



Part of process_form.php:

$username = $_REQUEST['username'];
 

The variable $username now has the value entered in the form's text field named 'username'

Simple
selection statement
if

if (exp)
    statement;

 

if-else

if (exp)
    statement;
else
    statement;

 

if-elseif

if (exp)
    statement;
elseif (exp)
    statement;
elseif (exp)
    statement;
else
    statement;

Creating your first
script to process a
form

You will need the pet form created in the form lecture: petform

Make sure the petform submit action sends it to your petform_ex1.php script

 

Try This:

The script should send a page back to the end user - recapping the info from the pet form.

Create the following php script in notepad:

<html>
<head>
<title>Pet Form Response</title>

<?php
$petname =$_REQUEST['petname'];
$petowner =$_REQUEST['petowner'];
$pettype =$_REQUEST['pettype'];
$gender =$_REQUEST['gender'];
$petstuff =$_REQUEST['petstuff'];
?>

</head>
<body>
 

<?php

  print("<br>The name of your pet is $petname <br>\n");
  print("<br>The owner of your pet is $petowner <br>\n");
  
?>


</body>
</html>

Save the file as petform_ex1.php

Upload it to Linux

Open the petform_ex1.htm using Internet Explorer

Fill in the information and submit the form

View source

Edit the file -> add the processing for gender and pettype

if ($pettype == 1)
    print("<br><br>You own a dog! <br> <br>\n\n");
elseif ($pettype == 2)
    print("<br><br>You own a cat! <br> <br>\n\n");
elseif ($pettype == "3")
    print("<br><br>You own a hamster!<br> <br>\n\n");
elseif ($pettype == "4")
    print("<br><br>You own a fish! <br> <br>\n\n");
elseif ($pettype == "5")
    print("<br><br>You own a bunny!<br> <br>\n\n");

if ($gender == "male")
    print("<br><br>Your pet is male <br> <br>\n\n");
else
    print("<br><br>Your pet is female <br> <br>\n\n");

 

Save the file as petform_ex1.php

Upload it to Linux

Open the petform_ex1.htm using Internet Explorer

Fill in the information and submit the form

View source

 

loops while

while (exp)
    statement;

 

do-while

do
{
    statement;
} while (exp);

 

for

for (exp1; exp2; exp3)
    statement;

 

foreach

- used only for arrays as a way to iterate through the array

foreach (array_exp as $value)
    statement

- will iterate through the array given in the array_exp assigning the array value to the variable $value

foreach (array_expression as $key => $value)
    statement

- will iterate through the array given in the array_exp assigning the array value's key to $value

 

processing a
checkbox

Try This:

edit the file petform_ex1.php -> add the processing for petstuff

 if (count($petstuff)>0)
    foreach($petstuff as $value)
    {
        print("<br><br>Your pet is $value <br> <br>\n\n");
    }

Save the file as petform_ex1.php

Upload it to Linux

Open the petform_ex1.htm using Internet Explorer

Fill in the information and submit the form

View source

  the php example: click here