PHP
In PHP there are three kinds of arrays.
1. Numeric Arrays - arrays that use numeric index
$var[0]=”data1”; $var[1]=”data2”; $var[2]=”data3”;
2. Associative Arrays - an array where each ID key is associated with a value.
$region[‘Cavite’] = “4A”; $region[‘Sorsogon’] = 5;
3. Multidimensional Arrays - array containing one or more arrays.
Most loops and functions in PHP uses the same structure as Java. They use the same key terms (while, for, if, else,) and the same rules in creating functions.
Loops:
while(condition){
statements…;
}
do{
statements;
}while(condition);
for(init; cond; increment){;}
foreach($aray as $value){;}
Decision Making:
if(condition){;}
if(condition){;}else{;}
switch (n){
case label1:
statements;break;
case label2:
statements;break;
default:
statements;
}
Functions:
function myFunc($param){
return $var;
}
Form Handling:
$_GET and $_POST are PHP variables used to retrieve information from HTML forms.
<html>
<body>
<form action=”welcome.php” method=”post”>
Name: <input type=”text” name=”fname” />
Age: <input type=”text” name=”age” />
<input type=”submit” />
</form>
</body>
</html>
The code above creates an HTML form. When the submit button is clicked the user input is sent over to the php file “welcome.php”
When we use the Get method, the data sent is seen by everyone on the URL. There is also a limit to the size of the information.
But when we use the Post method, data sent is invisible to others and has no size limit.
Welcome <?php echo $_POST[“fname”]; ?>!<br />
You are <?php echo $_POST[“age”]; ?> years old.
Data sent is placed on the arrays $_POST or $_GET. The $_REQUEST array contains all the data from the arrays $_POST, $_GET and $_COOKIES.
Date
The function date(format, timestamp) is used to format the date and time. It returns a string defined by the format parameter. The timestamp parameter provides a timestamp. If no timestamp is provided the current date and time is used.
Include and Required
You can insert the content of one php file to another by using include and require.
1. include() generates a warning, but the script will continue execution
2. require() generates a fatal error, and the script will stop
Opening a File
fopen() function is used to open files. The first parameter is the file being opened and the second parameter is the mode the file is to be opened.
<?php
$file=fopen(“welcome.txt”,”r”);
?>
Cookies
With PHP we can create and retrieve cookies. Cookies are often used to identify a user.
The setcookie() function is used to create a cookie. It should appear before the html tag.
Syntax: setcookie(name, user, expire, path, domain)
To retrieve the cookie we use the $_COOKIE variable.
example. <?php echo $_COOKIE[“user”]; ?>
The example above prints the value of the cookie named “user”.
isset() function is used to check if the cookie is set.
File Upload
PHP can be used to upload files.
<html>
<body>
<form action=”upload_file.php” method=”post” enctype=”multipart/form-data”>
<label for=”file”>Filename:</label>
<input type=”file” name=”file” id=”file” />
<br />
<input type=”submit” name=”submit” value=”Submit” />
</form>
</body>
</html>
Given the code above, on click of the submit button information of the file uploaded is sent to upload_file.php. The information of the file is saved in the variable $_FILE.
$_FILES[“file”][“name”] - the name of the uploaded file
$_FILES[“file”][“type”] - the type of the uploaded file
$_FILES[“file”][“size”] - the size in bytes of the uploaded file
$_FILES[“file”][“tmp_name”] - the name of the temporary copy of the file stored on the server
$_FILES[“file”][“error”] - the error code resulting from the file upload
Session
In PHP $_SESSION is a variable that can hold data about the user. It is open to all the pages in an application. A session is the period from the start of an application until it end.
Before storing information we must start the session using session_start(). It must appear before the html tag.
example.
<?php
session_start();
$_SESSION[‘userName’];
?>
<html><body>
<?php
echo “Welcome ” . $_SESSION[‘username’];
?>
</body></html>
The unset() function is used to free the specified session variable;
PHP has the mail() function that is used to send emails within the script.
Parameters: mail(to,subject,message,headers,parameters)
to - Required. Specifies the receiver / receivers of the email
subject - Required. Specifies the subject of the email.
message - Required. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters
headers - Optional. Specifies additional headers, like From, Cc, and Bcc.The additional headers should be separated with a CRLF (\r\n)
parameters - Optional. Specifies an additional parameter to the sendmail program