Algorithm_PHP
Algorithm_PHP
Aim:
Algorithm:
GET Method:
Step-1: Open the Notepad and create a HTML form with two input fields for
Numbers and submit button.
Step-2: Create another page named as “add.php” to display the result. Now, Use the
Method =”GET” attribute in the <form> tag in the input page.
Step-3: In the add.php, use $_GET[“n1”] and $_GET[“n2”] to retrive the values
entered in the form.
Step-4: Enter the values in the Input boxes and calculate the sum of two integers.
Step-5: Display the result using the ‘echo’ statement.
POST Method:
Step-1: Open the Notepad and create a HTML form with three input fields for
Name,Roll Number,Email and a submit button.
Step-2: Create another page named as “display.php” to display the result. Now, Use
the Method =”POST” attribute in the <form> tag in the input page.
Step-3: In the display.php, use $POST[“N”],$POST[“R”],and $_POST[“E”] to
retrieve the values entered in the form.
Step-4: Now,Enter the values name,roll no and email id and Display the values using
echo method in the display page.
Aim:
To create a PHP program to validate the user form.
Algorithm:
Step-1: Open the Notepad and Create an HTML form (user form.php) with fields for
Name, Contact No, E-mail, Gender, and Feedback.
Step-2: Use the ‘POST’ Method to submit the form to ‘feedback.php’.
Step-3: Ensure Contact No field is validated with a pattern for 10-digit numbers using,
(pattern="[0-9]{10}").
Step-4: In feedback.php, check if the form is submitted using the
(isset($_POST["Submit"])).
Step-5: Retrieve and validate each form field:
Name: Check if it's not empty and contains only alphabets
(preg_match("/^[a- zA-Z]*$/",$n)).
Contact No: Check if it's not empty and is a valid 10-digit number
(preg_match("/^[0-9]{10}+$/",$cno)).
E-mail: Check if it's not empty and is a valid email address (filter_var($e,
FILTER_VALIDATE_EMAIL)).
Gender: Check if it's selected.
If validation fails in any of the field then display appropriate error messages.
Step-6: If all fields pass validation, display the submitted values or a success message.
3. Array Function
Aim:
To create a PHP program to demonstrate the different predefined function in
array.
Algorithm:
Step-1: Open the notepad and Create an associative array $c with color names as
values and keys as color codes.
Step-2: Using the array sizeof($c), print the number of elements.
Step-3: Using array_values($c) & array_keys($c) to print the print the array values
and keys.
Step-4: Remove the last element of the array by using the array_pop($c) and print the
modified array.
Step-5: Next, sort the array using sort($c) and print the sorted array.
Step-6: Create an another array $ci with color names and merge it with the existing
array using array_merge($c, $ci).
Step-7: Finally, Select a random value from the array using $c[array_rand($c)] and
print it.
4. Math Function
Aim:
To create a PHP program to demonstrate the different predefined function in
Math.
Algorithm:
Aim:
To create a PHP program to demonstrate the different predefined function in
Date.
Algorithm:
Step-1: Open the Notepad and display the current date in various formats using the
date() function.
Step-2: To display the current day of the week use the date() function with “l” format
and check the current year is leap year or not with the “L” format.
Step-3: To display the number of days in the current month use the date function with
“t” format.
Step-4: To display the numeric representation of the current day of the week use the
date() function with “N” format and day of the year using “z” format.
Step-5: To display the current time in 12-hour format using the date() function with
the “h:i:sa” format.
Step-6: Then, use the mktime() function to create a specific date and display it in a
custom format and strtotime() function to create dates relative to the current
date and display them in a custom format.
Aim:
To write a PHP program to store current date-time in a COOKIE and display
the “Last visited on‟ date-time on the web page upon reopening of the same page.
Algorithm:
Step-1: Open the Notepad and create an HTML form with a submit button to display
the last visited time(store.php).
Step-2: Then,submit the form to another file(cookie.php) using the POST Method.
Step-3: Calculate the expiry time for the cookie(60 days from the current time.
Step-4: Now, set a cookie ‘lastvisit’ with the current date and time as its value and the
calculated expiry time.
Step-5: Check the ‘lastvisit’ cookie is set or not by sing the if statement.
Step-6: If the cookie is set and able to retrive then the last vist time will be displayed
with the value, if not set then this message will be displayed “you've got some
stale cookies”.
7. Session
Aim:
Algorithm:
Step-1: Open the Notepad and Create a HTML form(page.php) with the submit
button to view the page to check the views count.
Step-2: Submit the form to session.php using the POST Method to display the output.
Step-3: Then start a PHP session using session_start().
Step-4: Check the ‘views’ session to count the number of times by using the if
statement.
Step-5: If the set value increments by 1 then the value added to view times if not the
the view count will be 1.
Aim:
To Create a Cookie and add four user ID’s and passwords to the Cookie and
entered in the Login form and authenticate with the values (user id and passwords)
available in the cookies to identify valid user’s or not.
Algorithm:
Step-1: Open the notepad and create a HTML form(index.php) to accept the user
login credentials(i.e.,username and password)
Step-2: To display the output,submit the form to clock.php using the POST method.
Step-3: Then set cookies for users names(user1,user2,user3,user4) and
passwords(pass1,pass2,pass3,pass4).
Step-4: Use the $_POST method to retrieve the username and password from the
form.
Step-5: Check the submitted username and password match with the stored cookies
Step-6: If the match is found then the successfully logged in” message will be
displayed otherwise “Check your Username and password” message will be
displayed.
9. File Handling
Aim:
To create a PHP program to demonstrate the different file handling methods.
Algorithm:
Step-1: Open the Notepad and create a document called “demo.txt”.
Step-2: Now write a php program to open the text document demo.txt by using the $f .
Step-3: Use the fgets() function to Read the file line by line in a loop until the end of
the file once the end of the file reached then close the file by fclose() function.
Step-4: Again reopen the file in append mode and store the file and addend the strings
“hello” and “Welcome” using the fputs() function.
Step-5: now read the entire file using the array file() and print the array.
Step-6: Use the ftell() function to get nd print the current position of the file pointer
and move the file using fseek() function.
Step-7: To get and print the new position of the file using the ftell() function.
Step-8: Close the file.
Aim:
To create a PHP program to demonstrate the database connectivity.
Algorithm:
Step-1: Open the Notepad and create a HTML form (contact.php) to collect the
student details (name,roll number,mobile number).
Step-2: Display the output by submitting the form to home.php using the POST
method.
Step-3: In the home.php,Establish the database connection details (server
name,username,password and database name) and create a database and table.
Step-4: Then create a new MYSQLi connection object and check the connection
using the if statement.
Step-5: Retrieve the student details from the POST data and construct a SQL query to
insert the student details.
Step-6: If the details submited successfully then the new record inserted successfully
or the error message will be displayed.