Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
55 views

PHP Slips Solution

The document contains code snippets from multiple PHP slips. It includes examples of: 1. Counting vowels in a user-input string and displaying results. 2. Performing array operations like merge, intersection, union on associative arrays. 3. Generating a bill by inputting item details in a form and outputting a table. 4. Various PHP functions like string comparison, reversing substrings, checking if a number is prime. 5. Defining and accessing properties and methods of a class. 6. Form handling and function definitions for common string/array operations.

Uploaded by

dkpatil2929
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

PHP Slips Solution

The document contains code snippets from multiple PHP slips. It includes examples of: 1. Counting vowels in a user-input string and displaying results. 2. Performing array operations like merge, intersection, union on associative arrays. 3. Generating a bill by inputting item details in a form and outputting a table. 4. Various PHP functions like string comparison, reversing substrings, checking if a number is prime. 5. Defining and accessing properties and methods of a class. 6. Form handling and function definitions for common string/array operations.

Uploaded by

dkpatil2929
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

SLIP 1

Q1

<?php

phpinfo();

?>

Q2_1

<html>

<body>

<form action="" method="post">

Enter string : <input type="text" name="str">

<input type="submit" value="submit" name="submit">

</form>

<?php

function count_vowels($str){

$cnt_vowels = 0;

$vowels = array('a'=> 0,'e'=> 0,'i'=> 0,'o'=> 0,'u'=> 0);

for($c=0; $c<strlen($str) ; $c++){

if(key_exists($str[$c],$vowels)){

$cnt_vowels += 1;

$vowels[$str[$c]] += 1;

echo "Total Vowels : ".$cnt_vowels;

echo "<br>Occurences of Vowels : "; print_r($vowels);

if(isset($_POST['submit'])){

$str = $_POST['str'];
count_vowels($str);

?>

</body>

</html>

Q2_2

<?php

$arr1 = array("a"=>1,"b"=>2);

$arr2 = array("c"=>3,"b"=>4,"a"=>1);

echo "Array 1 : "; print_r($arr1);

echo "<br>Array 2 : "; print_r($arr2);

?>

<form action="" method="post">

<br>

<br><input type="radio" name="select" value="Merge">Merge

<br><input type="radio" name="select" value="Inter">Intersection

<br><input type="radio" name="select" value="Union">Union

<br><input type="radio" name="select" value="Set Difference">Set Difference

<br><br><input type="submit" value="submit" name="submit">

</form>

<?php

if(isset($_POST["submit"])){

$ope = $_POST['select'];

switch($ope){

case 'Merge':

$res = array_merge($arr1,$arr2);

break;

case 'Inter':

$res = array_intersect_assoc($arr1,$arr2);
break;

case 'Union':

$res = array_merge($arr1,$arr2);

break;

case 'Set Difference':

$res = array_diff_assoc($arr1,$arr2);

break;

print_r($res);

?>

SLIP 2

Q1

<?php

$id = 101;

$name = "Rahul";

$age = 20;

$add = "Pune";

echo "<br> ID : $id";

echo "<br> Name : $name";

echo "<br> Age : $age";

echo "<br> Address : $add";

?>

Q2_1

<?php

function calc($n1,$n2,$ope = "+"){

switch($ope){

case '+':

return $n1 + $n2;


case '-':

return $n1 - $n2;

case '*':

return $n1 * $n2;

case '/':

return $n1 / $n2;

if(isset($_POST["submit"])){

$n1 = $_POST["n1"];

$n2 = $_POST["n2"];

$ope = $_POST["ope"];

$res = calc($n1,$n2,$ope);

echo "Result : $res";

?>

SLIP 3

Q2_1

<h2>Bill Details</h2>

<form method="post">

<table>

<tr>

<th>Item Code</th>

<th>Item Name</th>

<th>Units Sold</th>

<th>Rate</th>

</tr>

<?php
for ($i=1; $i<=5; $i++) {

echo "<tr>";

echo "<td><input type='text' name='item[$i][code]'></td>";

echo "<td><input type='text' name='item[$i][name]'></td>";

echo "<td><input type='text' name='item[$i][units]'></td>";

echo "<td><input type='text' name='item[$i][rate]'></td>";

echo "</tr>";

?>

</table>

<input type="submit" value="Generate Bill">

</form>

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$items = $_POST['item'];

echo "<h2>Bill</h2>";

echo "<table>";

echo "<tr><th>Item Code</th><th>Item Name</th><th>Units Sold</th><th>Rate</th><th>Total


Amount</th></tr>";

foreach ($items as $item) {

$code = $item['code'];

$name = $item['name'];

$units = $item['units'];

$rate = $item['rate'];

$total = $units * $rate;

echo "<tr>";

echo "<td>$code</td><td>$name</td><td>$units</td><td>$rate</td><td>$total</td>";
echo "</tr>";

echo "</table>";

?>

SLIP 4

hey there what's up..!!

Q1

<?php

$n1 = 10;

$n2 = 20;

$n3 = 30;

$max = $n1>$n2 ? ($n1>$n3 ? $n1 : $n3) : ($n2>$n3 ? $n2 : $n3);

echo "Maximum : $max";

?>

Q2_1

<form action="" method="post">

String 1 : <input type="text" name="s1"><br>

String 2 : <input type="text" name="s2"><br>

<br>

<input type="radio" name="select" value="1">Compare <br>

<input type="radio" name="select" value="2">Append <br>

<input type="radio" name="select" value="3">Reverse

<input type="number" name="pos"><br>

<br>

<input type="submit" name="submit" value="submit">

</form>
<?php

if(isset($_POST["submit"])){

$str1 = $_POST["s1"];

$str2 = $_POST["s2"];

$ope = $_POST['select'];

if($ope == '1'){

if($str1 == $str2){

echo "String are equal using '==' operator";

else{

echo "String are not equal using '==' operator";

if(strcmp($str1,$str2)==0){

echo "<br>String are equal using 'strcmp' function";

else if(strcmp($str1,$str2) < 0){

echo "<br>The first string is less than the second string using 'strcmp' function";

else {

echo "<br>The first string is greater than the second string using 'strcmp' function";

else if($ope == '2'){

$string = $str1.$str2;

echo "Appended String : ".$string;

else if($ope == 3){


$pos = $_POST['pos'];

$rev_string = substr($str1,0,$pos) . strrev(substr($str1,$pos));

echo "Reverse String from specified position : ".$rev_string;

?>

Q2_2

<html>

<head>

<script>

function readTextFile() {

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {

if (xhr.readyState === 4 && xhr.status === 200) {

document.getElementById('output').innerHTML =
xhr.responseText;

xhr.open('GET', 'a.txt');

xhr.send();

</script>

</head>

<body>

<button onclick="readTextFile()">Print Text File</button>

<div id="output"></div>

</body>

</html>

SLIP 5

Q1

<?php
$num = 153;

$temp = $num;

$sum = 0;

$power = strlen($temp);

while($temp > 0){

$rem = $temp%10;

$sum += pow($rem,$power);

$temp /= 10;

if($sum == $num){

echo "$num is a armstrong number";

else{

echo "$num is not a armstrong number";

?>

Q2_1

<form action="" method="post">

<input type="radio" name="select" value="1">Display Array <br>

<input type="radio" name="select" value="2">Display Array Size

<br><br>

<input type="submit" name="submit" value="submit">

</form>

<?php

$arr = array('a'=>1, 'b'=>2);

if(isset($_POST["submit"])){
$select = $_POST["select"];

switch($select){

case '1':

print_r($arr);

break;

case '2':

echo sizeof($arr);

break;

?>

Q2_2

<?php

class Myclass{

public $property1 = 'Hello';

public $property2 = 'World';

public function method1(){

echo "method 1..!!";

public function method2(){

echo "method 2..!!";

};

$obj = new Myclass();

echo "<br>Object's class name : ".get_class($obj);


$properties = get_object_vars($obj);

echo "<br>Properties : <br>";

foreach($properties as $key=>$value){

echo "$key => $value <br>";

$methods = get_class_methods(get_class($obj));

echo "<br>Methods : <br>";

foreach($methods as $m){

echo "$m <br>";

if(property_exists($obj,'property1')){

echo "<br>Object has property1";

if(method_exists($obj,'method1')){

echo "<br>Object has method1";

?>

SLIP 6

Q1

<form action="" method="post">

<input type="number" name="num">

<br><br>

<input type="submit" name="submit" value="submit">

</form>

<?php

function isPrime($num){
if($num<2){

return False;

for($i=2;$i<$num;$i++){

if($num%$i == 0){

return False;

return True;

if(isset($_POST["submit"])){

$num = $_POST["num"];

if(isPrime($num)){

echo "$num is Prime Number..!!";

else{

echo "$num is not a Prime Number ..!!";

?>

Q2_1

<?php

$arr = array('a'=>1, 'b'=>2 , 'c'=>3);

print_r($arr);

?>

<form action="" method="post">

<br><input type="radio" name="select" value="1">Reverse key-value pair

<br><input type="radio" name="select" value="2">Traverse in random order


<br><br>

<input type="submit" name="submit" value="submit">

</form>

<?php

if(isset($_POST["submit"])){

$select = $_POST["select"];

switch($select){

case '1':

print_r(array_flip($arr));

break;

case '2':

shuffle($arr);

print_r($arr);

break;

?>

SLIP 7

Q1

<form action="" method="post">

<input type="text" name="str">

<br><br>

<input type="submit" name="submit" value="submit">

</form>

<?php

function reverse($str){
return strrev($str);

if(isset($_POST["submit"])){

$str = $_POST["str"];

$newString = reverse($str);

echo "$newString";

?>

Q2_1

<?php

$arr = array('a'=>1, 'b'=>2 , 'c'=>3);

$reversedArray = array_reverse($arr);

print_r($reversedArray);

?>

SLIP 8

Q1

<form action="" method="post">

<input type="text" name="str">

<br><br>

<input type="submit" name="submit" value="submit">

</form>

<?php

function isPalindrome($str){

if($str == strrev($str)){

return True;

return False;

}
if(isset($_POST["submit"])){

$str = $_POST["str"];

if(isPalindrome($str)){

echo "$str is palindrome..!!";

else{

echo "$str is not a palindrome..!!";

?>

Q2_1

<?php

$fruits = array(

"apples" => array(

"green" => 5,

"red" => 10,

"yellow" => 3

),

"oranges" => array(

"navel" => 8,

"valencia" => 12

),

"bananas" => array(

"yellow" => 15,

"red" => 2

);

echo "Number of red apples: " . $fruits["apples"]["red"] . "<br>";


unset($fruits["oranges"]["navel"]);

print_r($fruits);

?>

SLIP 0

Q1

<?php

$num=1;

for ($i = 1; $i <= 4; $i++) {

for ($j = 1; $j <= $i; $j++) {

echo $num . " ";

$num++;

echo "<br>";

?>

Q2_1

<form action="" method="post">

<br><input type="radio" name="select" value="insert" checked>Insert <input type="text"


name="ele">

<br><input type="radio" name="select" value="delete">Delete

<br><br>

<input type="submit" name="submit" value="submit">

</form>

<?php

session_start();

if(!isset($_SESSION['stack_array'])){

$_SESSION['stack_array'] = array();

$s = array();
}

else{

$s = $_SESSION['stack_array'];

if(isset($_POST["submit"])){

$ope = $_POST["select"];

switch($ope){

case 'insert':

$ele = $_POST["ele"];

array_push($s,$ele);

echo "$ele inserted in stack..!!";

break;

case 'delete':

if(empty($s)){

echo "Stack is already empty..!1";

else{

array_pop($s);

echo "Element removed from stack..!!";

echo "<br><br>Stack Elements : <br>";

foreach($s as $ele){

echo "$ele <br>";

$_SESSION['stack_array'] = $s;

}
?>

SLIP 10

Q1

<?php

echo show_source("temp.html");

?>

Q2_1

<form action="" method="post">

<br><input type="radio" name="select" value="insert" checked>Insert

<input type="text" name="ele">

<br><input type="radio" name="select" value="delete">Delete

<br><input type="radio" name="select" value="display">Display

<br><br>

<input type="submit" name="submit" value="submit">

</form>

<?php

session_start();

if(!$_SESSION['queue']){

$q = array();

else{

$q = $_SESSION['queue'];

if(isset($_POST['submit'])){

$ope = $_POST['select'];

switch($ope){

case 'insert':

$ele = $_POST['ele'];

array_push($q,$ele);
echo "$ele inserted in queue..!!";

break;

case 'delete':

if(empty($q)){

echo "Queue is already empty..!!";

break;

array_shift($q);

echo "Element removed from queue..!!";

break;

case 'display':

if(empty($q)){

echo "Queue is empty..!!";

break;

echo "Queue Element : ";

foreach($q as $i){

echo $i.", ";

$_SESSION['queue'] = $q;

?>

Temp

<html>

<h1>Hey There..!!</h1>

</html>

SLIP 11

Q2_1
<html>

<body>

<form action="" method="post">

Enter string : <input type="text" name="str">

<input type="submit" value="submit" name="submit">

</form>

<?php

function count_vowels($str){

$cnt_vowels = 0;

$vowels = array('a'=> 0,'e'=> 0,'i'=> 0,'o'=> 0,'u'=> 0);

for($c=0; $c<strlen($str) ; $c++){

if(key_exists($str[$c],$vowels)){

$cnt_vowels += 1;

$vowels[$str[$c]] += 1;

echo "Total Vowels : ".$cnt_vowels;

echo "<br>Occurences of Vowels : "; print_r($vowels);

if(isset($_POST['submit'])){

$str = $_POST['str'];

count_vowels($str);

?>

</body>

</html>

SLIP 14
Q2_1

<form action="" method="post">

String 1 : <input type="text" name="s1"><br>

String 2 : <input type="text" name="s2"><br>

<br>

<input type="radio" name="select" value="1">Compare <br>

<input type="radio" name="select" value="2">Append <br>

<input type="radio" name="select" value="3">Reverse

<input type="number" name="pos"><br>

<br>

<input type="submit" name="submit" value="submit">

</form>

<?php

if(isset($_POST["submit"])){

$str1 = $_POST["s1"];

$str2 = $_POST["s2"];

$ope = $_POST['select'];

if($ope == '1'){

if($str1 == $str2){

echo "String are equal using '==' operator";

else{

echo "String are not equal using '==' operator";

if(strcmp($str1,$str2)==0){

echo "<br>String are equal using 'strcmp' function";

else if(strcmp($str1,$str2) < 0){


echo "<br>The first string is less than the second string using 'strcmp' function";

else {

echo "<br>The first string is greater than the second string using 'strcmp' function";

else if($ope == '2'){

$string = $str1.$str2;

echo "Appended String : ".$string;

else if($ope == 3){

$pos = $_POST['pos'];

$rev_string = substr($str1,0,$pos) . strrev(substr($str1,$pos));

echo "Reverse String from specified position : ".$rev_string;

?>

SLIP 15

Q2_2

<?php

class Myclass{

public $property1 = 'Hello';

public $property2 = 'World';

public function method1(){

echo "method 1..!!";

public function method2(){


echo "method 2..!!";

};

$obj = new Myclass();

echo "<br>Object's class name : ".get_class($obj);

$properties = get_object_vars($obj);

echo "<br>Properties : <br>";

foreach($properties as $key=>$value){

echo "$key => $value <br>";

$methods = get_class_methods(get_class($obj));

echo "<br>Methods : <br>";

foreach($methods as $m){

echo "$m <br>";

if(property_exists($obj,'property1')){

echo "<br>Object has property1";

if(method_exists($obj,'method1')){

echo "<br>Object has method1";

?>

SLIP 18

Q2_2

<form action="" method="post">


<input type="text" name="oname">

<br><br>

<input type="submit" name="submit" value="submit">

</form>

<?php

$db = pg_connect("host=localhost user = postgres dbname = rahul");

if(isset($_POST["submit"])){

$oname = $_POST["oname"];

$sql = "select * from property where oname = '$oname'";

$res = pg_query($db,$sql);

echo "<table border = 1>";

while($row = pg_fetch_row($res)){

echo "<tr> <td> $row[0] </td><td> $row[1] </td><td>$row[2]</td></tr>";

echo "</table>";

pg_close($db);

?>

SLIP 10

Q1

<?php

$num = 65;

for($i=1 ; $i<=4 ; $i++){

for($j=1 ; $j<=$i ; $j++){

$alphabet = chr($num);

echo $alphabet." ";


$num++;

echo "<br>";

?>

You might also like