0% found this document useful (0 votes)
254 views

Sop3 - PHP Programs

The document describes a PHP program to perform operations on an associative array. It explains how to display array elements with keys, get the array size, and delete an element from the array at a given index. Code examples are provided to demonstrate each operation.

Uploaded by

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

Sop3 - PHP Programs

The document describes a PHP program to perform operations on an associative array. It explains how to display array elements with keys, get the array size, and delete an element from the array at a given index. Code examples are provided to demonstrate each operation.

Uploaded by

avnishdhumal2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT NO 10

Write a PHP program to check if a person is eligible to vote or not. Theprogram


should include the following-

Minimum age required for vote is 18.


Use PHP functions.
Use Decision making statement.

Theory:

The PHP Hypertext Preprocessor (PHP) is a programming language that allows


web developers to create dynamic content that interacts with databases. PHP is
basically used for developing web based software applications. This tutorial helps
you to build your base with PHP.

Characteristics of PHP

Five important characteristics make PHP's practical nature possible −

 Simplicity
 Efficiency
 Security
 Flexibility
 Familiarity

Hello World using PHP

<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php echo "Hello, World!";?>
</body>
</html>
SOP1 PRACTICAL (PHP) 1

<!DOCTYPE html>
<html>
<head>
<title>Eligible to Vote or not</title>
</head>
<body>
<form action="" method="post"> Enter a
no :
<input type="text" name="t1" placeholder="Enter a number">
<br><input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['submit'])) {vote();
}
function vote() {
$a = $_POST['t1'];
intval($a); if($a>=18){
echo "You are Eligible for Vote";
}
else{
echo "You are not Eligible for Vote";
}
}
?>
</body>
</html>

OUTPUT:
EXPERIMENT NO 11

Write a PHP function to count the total number of vowels (a,e,i,o,u) from thestring.
Accept a string by using HTML form.

THEORY:

The count() function returns the number of elements in an array.

Vowels
The letters A, E, I, O, and U are called vowels. The other letters in thealphabet are
called consonants.

More about Vowels


 The word Iouea (a genus of sea sponges) contains all five vowels and no
other letters.
(Being the name of a genus (i.e., a proper noun), it is written with acapital
letter. Also of note, it is the shortest word with four syllables.)
 The words abstemious and facetious contain all five vowels in
order.

A vowel is classified as "a speech sound produced by a comparatively open


configuration of the vocal tract, with vibration of the vocal cords but without
audible friction". So what? Well, using this definition, the letter Y in words like hymn
and shy is also a vowel. However, in words like beyond and yes, Yis a consonant
because the breath is partly obstructed.
SOP2 PRACTICAL (PHP) 2

<html>
<body>
<h2>Find Number of Vowels in a String</h2>
<form action="" method="post">
<input type="text" name="string" />
<input type="submit" />
</form>
</body>
</html>
<?php
if($_POST)
{
$string = strtolower($_POST['string']);
$vowels = array('a','e','i','o','u');
$len = strlen($string);
$num = 0;
for($i=0; $i<$len; $i++){
if(in_array($string[$i], $vowels))
{
$num++;
}
}
echo "Number of vowels : ".$num;
}

?>

OUTPUT
EXPERIMENT NO 12

Write a PHP program to perform the following operations on an associative array.


 Display elements of an array along with their keys.
 Display the size of an array.
 Delete an element from an array from the given index.

Theory:

PHP Arrays
An array is a special variable, which can hold more than one value at a time.
An array stores multiple values in one single variable:

Create an Array in PHP :


In PHP, the array() function is used to create an array.
Syntax :
$a = array( values )

In PHP, there are three types of arrays:


1.Indexed arrays - Arrays with a numeric index
2.Associative arrays - Arrays with named keys
3.Multi-dimensional arrays – Arrays containing one or more arrays

Indexed arrays :
The index can be assigned automatically (index always starts at 0).

Syntax :
$a = array( value1, value2, ..., value n)

Associative Arrays :
Associative arrays are arrays that use named keys instead of index to identify
record/value. Let us see how to create associative array.
Syntax :
$a = array( key1 => value1, key2 =>value2, ...,key n => value n)

Multi-dimensional Arrays :
A multidimensional array is an array containing one or more arrays. PHP can handle
multiple levels of multidimensional array.
SOP3 PRACTICAL (PHP) 3

<?php
$ml=
array("English"=>"55",
"Hindi"=>"60",
"Maths"=>"70",
"Marathi"=>"85");
echo "<br><br><b>Elements of an array along with their keys:</b>";
echo "<br> <br> Your score ".$ml['English']." in English";
echo "<br> <br> Your score ".$ml['Hindi']." in Hindi";
echo "<br> <br> Your score ".$ml['Maths']." in Maths";
echo "<br> <br> Your score ".$ml['Marathi']." in Marathi";
echo "<br><br><b>Size of an array is :</b>".count($ml);
array_splice($ml,0,1);
echo "<br><br><b>After deleting array is :</b>";
foreach($ml as $x => $x_value)
{
echo "<br><br>Key=". $x. ", Value=" . $x_value;
echo "<br>";
}
?>

Output:

You might also like