0% found this document useful (0 votes)
5 views14 pages

Basic PHP Program

The document contains a series of PHP programs demonstrating various functionalities such as embedding PHP in HTML, creating variables, using echo statements, and handling strings. It also includes examples of working with arrays, arithmetic operations, cookies, and form validation. Each program is accompanied by its corresponding HTML code and expected output.

Uploaded by

Tungare Virshree
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

Basic PHP Program

The document contains a series of PHP programs demonstrating various functionalities such as embedding PHP in HTML, creating variables, using echo statements, and handling strings. It also includes examples of working with arrays, arithmetic operations, cookies, and form validation. Each program is accompanied by its corresponding HTML code and expected output.

Uploaded by

Tungare Virshree
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PHP Programs

1. Embedding PHP within HTML


Code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>A Simple PHP File</title>
</head>
<body>
<h1><?php echo "Hello, world!"; ?></h1>
</body>
</html>
2. Write a program to create variable in php
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Variables</title>
</head>
<body>

<?php
$txt = "Hello World!";
$number = 10;

// Display variables value


echo $txt;
echo "<br>";
echo $number;
?>

</body>
</html>
Out put :
Hello World!
10
3. Write a php Script for using echo with string
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP echo Statement</title>
</head>
<body>

<?php
// Displaying string of text
echo "Hello, I am using echo command !";
?>

</body>
</html>

Output
Hello, I am using echo command
4. Write a script to show the Differences between single and
double quoted strings
Ans : <!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Single and Double Quoted Strings</title>
</head>
<body>

<?php
$my_str = 'World';
echo "Hello, $my_str!<br>";
echo 'Hello, $my_str!<br>';

echo '<pre>Hello\tWorld!</pre>';
echo "<pre>Hello\tWorld!</pre>";
echo 'I\'ll be back';
?>

</body>
</html>

Output :
Hello, World!
Hello, $my_str!
Hello\tWorld!
Hello World!

I'll be back
5. Script to Find the length of a string
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Calculate String Length</title>
</head>
<body>

<?php
$my_str = 'Welcome to Tutorial Republic';

// Calculating and displaying string length


echo strlen($my_str);
?>

</body>
</html>
Output : 28
6. Script to Reversing the direction of a string
Ans :
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Reversing a String</title>
</head>
<body>

<?php
$my_str = 'You can do anything, but not everything.';

// Display reversed string


echo strrev($my_str);
?>

</body>
</html>
Output :
.gnihtyreve ton tub ,gnihtyna od nac uoY
7. Program with arithmetic operators
Ans :
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Arithmetic Operators</title>
</head>
<body>

<?php
$x = 10;
$y = 4;

echo($x + $y);
echo "<br>";

echo($x - $y);
echo "<br>";

echo($x * $y);
echo "<br>";

echo($x / $y);
echo "<br>";

echo($x % $y);
?>

</body>
</html>
Output :
14
6
40
2.5
2
8. Script to check if variable is numeric or not
Ans :
<!DOCTYPE html>
<html>
<body>

<?php
// Check if the variable is numeric
$x = 5985;
var_dump(is_numeric($x));

echo "<br>";

$x = "5985";
var_dump(is_numeric($x));

echo "<br>";

$x = "59.85" + 100;
var_dump(is_numeric($x));

echo "<br>";

$x = "Hello";
var_dump(is_numeric($x));
?>

</body>
</html>

9. Example of Associative Array


Ans :
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Associative Array</title>
</head>
<body>

<?php
$ages = array("Peter"=>22, "Clark"=>32, "John"=>28);

// Printing array structure


print_r($ages);
?>

</body>
</html>
Output :
Array ( [Peter] => 22 [Clark] => 32 [John] => 28 )
10. Script to Sorting PHP Associative Array in Ascending Order by
Value
Ans :
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sorting PHP Associative Array in Ascending Order by
Value</title>
</head>
<body>

<?php
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);

// Sorting array by value and print


asort($age);
print_r($age);
?>

</body>
</html>
Output :
Array ( [Harry] => 14 [Peter] => 20 [Clark] => 35 [John] => 45 )
11. Script to Sorting PHP Associative Array in Descending Order by
Key
Ans :
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sorting PHP Associative Array in Descending Order by
Key</title>
</head>
<body>

<?php
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);

// Sorting array by value and print


krsort($age);
print_r($age);
?>

</body>
</html>
Output :
Array ( [Peter] => 20 [John] => 45 [Harry] => 14 [Clark] => 35 )
12. Find the Sum of even and odd numbers between 1 to 100.
Ans :
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Associative Array</title>
</head>
<body>
<?php

for ($i=1; $i<=100; $i++)

if($i%2==0)

@$even=$even+$i;

else

@$odd=$odd+$i;

echo "Sum of even numbers=".$even."<br/>";

echo "Sum of odd numbers=".$odd;

?>
</body>
</html>
Out put
Output
Sum of even numbers=2550
Sum of odd numbers=2500
13. PHP Script to create and retrieve a cookie
Ans : <!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not
set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

<p><strong>Note:</strong> You might have to reload the


page to see the value of the cookie.</p>

</body>
</html>
Cookie named 'user' is set!

14. Code to select a data with mysql


Ans :
<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ".
$row["firstname"]. " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}

$conn->close();
?>

</body>
</html>
Output :
id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
15. Code to validate a form
Ans :
<HTML>
<BODY>
<FORM METHOD="POST" ACTION="[Link]">
<H1>Contact Information</H1>
<TABLE>

<TR>
<TD><B>Nickname:</B></TD>
<TD><INPUT TYPE="TEXT" NAME="nickname"></TD>
</TR>

<TR>
<TD>Title:</TD>
<TD><INPUT TYPE="TEXT" NAME="title"></TD>
</TR>

<TR>
<TD><B>First Name:</B></TD>
<TD><INPUT TYPE="TEXT" NAME="firstname"></TD>
</TR>

<TR>
<TD>Middle Name:</TD>
<TD><INPUT TYPE="TEXT" NAME="middlename"></TD>
</TR>

<TR>
<TD><B>Last Name:</B></TD>
<TD><INPUT TYPE="TEXT" NAME="lastname"></TD>
</TR>

<TR>
<TD><B>Primary Email:</B></TD>
<TD><INPUT TYPE="TEXT" NAME="email"></TD>
<TD WIDTH="20">&nbsp;</TD>
<TD>Secondary Email:</TD>
<TD><INPUT TYPE="TEXT" NAME="secondaryemail"></TD>
</TR>

<TR>
<TD>Company Name:</TD>
<TD><INPUT TYPE="TEXT" NAME="companyname"></TD>
</TR>

<TR>
<TD>Office Address:</TD>
<TD><INPUT TYPE="TEXT" NAME="officeaddres1"></TD>
<TD WIDTH="20">&nbsp;</TD>
<TD>Home Address:</TD>
<TD><INPUT TYPE="TEXT" NAME="homeaddress"></TD>
</TR>

<TR>
<TD></TD>
<TD><INPUT TYPE="TEXT" NAME="officeaddress2"></TD>
</TR>

<TR>
<TD>City:</TD>
<TD><INPUT TYPE="TEXT" NAME="officecity"></TD>
<TD WIDTH="20">&nbsp;</TD>
<TD>&nbsp;</TD>
<TD><INPUT TYPE="TEXT" NAME="homecity"></TD>
</TR>

<TR>
<TD>State:</TD>
<TD><INPUT TYPE="TEXT" NAME="officestate"></TD>
<TD WIDTH="20">&nbsp;</TD>
<TD>&nbsp;</TD>
<TD><INPUT TYPE="TEXT" NAME="homestate"></TD>
</TR>

<TR>
<TD>Zip:</TD>
<TD><INPUT TYPE="TEXT" NAME="officezip"></TD>
<TD WIDTH="20">&nbsp;</TD>
<TD>&nbsp;</TD>
<TD><INPUT TYPE="TEXT" NAME="homezip"></TD>
</TR>

<TR>
<TD>Phone:</TD>
<TD><INPUT TYPE="TEXT" NAME="officephone"></TD>
<TD WIDTH="20">&nbsp;</TD>
<TD>&nbsp;</TD>
<TD><INPUT TYPE="TEXT" NAME="homephone"></TD>
</TR>

<TR>
<TD>Birthday:</TD>
<TD><INPUT TYPE="TEXT" NAME="birthday"></TD>
</TR>

<TR>
<TD>Spouse Name:</TD>
<TD><INPUT TYPE="TEXT" NAME="spousename"></TD>
<TD WIDTH="20">&nbsp;</TD>
<TD>Childrens' Names:</TD>
<TD><INPUT TYPE="TEXT" NAME="children"></TD>
</TR>

<TR>
<TD>Anniversary:</TD>
<TD><INPUT TYPE="TEXT" NAME="anniversary"></TD>
</TR>

</TABLE>

<BR>
<BR>
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
<BR>
<BR>
<INPUT TYPE="RESET" VALUE="Clear the Form">

</FORM>
</BODY>
</HTML>
<!-- [Link]
<HTML>
<BODY>
<?php

$errors=0;
if (!trim($nickname)) {
echo "<BR><B>Nickname</B> is required.";
$errors++;
}

if (!trim($firstname)) {
echo "<BR><B>First name</B> is required.";
$errors++;
}

if (!trim($lastname)) {
echo "<BR><B>Last name</B> is required.";
$errors++;
}

if (!trim($email)) {
echo "<BR><B>Primary email address</B> is required.";
$errors++;
}

if ($errors > 0)
echo "<BR><BR><BR>Please use your browser's back button " .
"to return to the form, and correct error(s)";

?>

</BODY>
</HTML>
-->

You might also like