Chapter 3:: PHP Solved Questions

CkRe0ObWYAAzroM

Contributed by:Mahgul Afzali

Let,s begin from the scratch where we can lead you on the practice of these languages.This is a very useful matreail on php which you can go through it and learn the php . Perhaps you can go to any of the websites where you have to spend a hefty amount of time . But here at Trackcode.com we provide functional and easy to understand PHP tutorial for beginners with solved examples.

 

This is an advantage of the PHP tutorial for PHP interested students as well as the experts of PHP programming who can learn many more things through this site.

Q1)Create a script using a for loop to add all the

integers between 0 and 30 and display.

for($i=0;$i<=30;$i++){

$sum=0;

Ssum=$sum+$i;

echo $sum;

}

?>

<html>

Q3 )$var = ‘PHP Tutorial’. Put this variable into the title section,

h3 tag and as an anchor.

<?php

$var=’php Tutorial’;

echo “<h3>$var</h3>”

?>

Q4)Write a PHP program which iterates the integers from 1 to 100.

For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers

which are multiples of both three and five print “FizzBuzz”.

<?php

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

{

if ( $i%3 == 0 && $i%5 == 0 )

{

echo $i . ” FizzBuzz”.”\n” ;

}

else if ( $i%3 == 0 )

{

echo $i. ” Fizz”.”\n”;

}

else if ( $i%5 == 0 )

{

echo $i. ” Buzz”.”\n”;

}

else

{

echo $i.”\n”;

}

}

?>

Q5)Create a script using a for loop to add all the

integers between 0 and 30 and display.

<?php

for($i=0;$i<=30;$i++){

$sum=0;

$sum=$sum+$i.” “;

echo $sum;

}

?>

Q6)

$text=”w3resource”;

$search_char=”r”;

$count=”0″;

for($i=”0″; $i< strlen($text); $i++)

{

if(substr($text,$i,1)==$search_char)

{

$count=$count+1;

}

}

echo $count.”\n”;

Q7)write a php script to display table of numbers.

<?php

echo”<table border=’1′, cellpading=’3′,cellspacing=’0′>”;

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

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

echo “<tr><td>”.” “;

echo “$i*$j”.”=”.$i*$j.” “;

echo”</td></tr>”;

}

}

echo”</table>”;

?>

Q8)write a php script to swap to number.

<?php

function swap(&$no1,&$no2){

echo “befor swaping $no1″.”<br/>”;

echo “befor swaping $no2”;

$result=$no1;

$no1=$no2;

$no2=$result;

}

$no1=2;

$no2=3;

swap($no1,$no2);

echo “After swaping $no1″.”<br/>”;

echo “After swaping $no2”;

?>

Q9)Write a php script to display factorial of a number.

<?php

function fact($no){

//$factorial=$factorial*$no;

if($no>0)

return 1;

$fact=$no*$fact*($no-1);

}

echo fact(12);

?>

<?php

Q10)Create a script to construct the following pattern, using nested for loop.

*  
* *  
* * *  
* * * *  
* * * * *

<?php

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

{

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

{

echo “*”;

if($j< $i)

{

echo ” “;

}

}

echo “\n”;

}

?>

Chapter 4 variables:

All variables in PHP start with a $ (dollar) sign ollowed by the name of the variable.

A valid variable name starts with a letter (A-Z, a-z) or underscore (_), followed by any number of letters, numbers, or underscores.

If a variable name is more than one word, it can be separated with underscore (for example $employee_code instead of $employeecode).

‘$’ is a special variable that can not be assigned.

EX:

<?php

$var=”Hello Wold”;

echo $var;

?>

PHP is a loosely type language

In language such as C, C++ and Java the programmer must declare the name and type of the variable before use it.In PHP the type of the variable does not need to be declared before use it,because types are associated with values rather than variables. As a result a variable can change the type of its value as much as we want.

As previously mentioned you don’t need to declare variables or their type before using them in PHP. In the following example none of the variables are declared before they are used, fact is $height is floating number and $width is an integer.

EX:

<?php

$height=2.3;

$width=23;

$Area=height*width;

echo “The area is “,$area;

?>

Variable Scope

Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types −

  • Local variable
  • Global variable
  • Static variable

Local Variables

A variable declared in a function is considered local; that is, it can be referenced solely in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function −

EX:

<?php
$x = 4;

function assignx () {
$x = 0;
print “$x inside function is $x. <br />”;
}

assignx();
echo ” $x outside of function is $x. <br />”;
?>

Global Variables

In contrast to local variables, a global variable can be accessed in any part of the program. However, in order to be modified, a global variable must be explicitly declared to be global in the function in which it is to be modified. This is accomplished, conveniently enough, by placing the keyword GLOBAL in front of the variable that should be recognized as global. Placing this keyword in front of an already existing variable tells PHP to use the variable having that name. Consider an example −

<?php
$var = 20;

function addit() {
GLOBAL $var;
$var++;

print “var is $var”;
}

addit();
?>

Static Variables

The final type of variable scoping that I discuss is known as static. In contrast to the variables declared as function parameters, which are destroyed on the function’s exit, a static variable will not lose its value when the function exits and will still hold that value should the function be called again.

You can declare a variable to be static simply by placing the keyword STATIC in front of the variable name.

<?php
function Count_num() {
STATIC $count = 0;
$count++;
echo $count;
echo  “<br />”;
}
Count_num();

Count_num();

Count_num();
?>

Function Parameters:

Function parameters are declared after the function name and inside parentheses. They are declared much like a typical variable would be −

<?php
// multiply a value by 10 and return it to the caller
function multiply ($value) {
$value = $value * 10;
return $value;
}

$retval = multiply (10);
Print “Return value is $retval\n”;
?>

About the author: Mahgul Afzali

Leave a Reply

Your email address will not be published.