Thursday, 21 November 2024

PHP Basic-2

 Example-1


<!DOCTYPE html>

<html>

  <head>

    <title></title>

  </head>

  <body bgcolor="white">

  <?php

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

{

        $num=$_POST['num1'];

        define('NUM',$num);


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

        {

            echo $i*NUM;

            echo '</br>';

        }

}

  ?>

<form name="f1" method="post" action="#">

Enter Number<input type="text" name="num1" size="25" required>

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

</form>

</body>

</html>


 Example-2

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php

$x=8;
if($x>5)
{
    echo "$x is greater than 5</br>";
}
if($x<10)
{
    echo "$x is also less than 10</br>";

}

  ?>

 Example-3

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">
  <?php
      $a=80;
      echo "your grade is:";
      switch($a)
      {
          case $a>=90:
            echo "A</br>";
          break;

          case $a>=80:
            echo "B</br>";
          break;

          case $a>=70:
            echo "C</br>";
          break;

          default
          echo "you have fail";
         break;
      }
  ?>
</body>
</html>


 Example-4

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php

      $a=10;

      while($a>=0)
      {
          echo"no is".$a."\n";
          $a--;

      }
     

  ?>

  <?php
          echo "hello world".$a;
          print ""

  ?>


 Example-5

<?php
          if(isset($_POST['submit']))
          {
            
           $num1=$_POST['val1'];

           if($num1%2==0)
              {
                  echo "Even No.";
              }
              else
              {
                  echo "Odd No.";
              }  
            }
          
   ?>

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <form name="f1" action="#" method="post">

Enter No.<input type="text" name="val1" size="20">

<input type="submit" name="submit" value="check">
        </form>


   </body>
   </html>
   

 Example-6

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>

  <body bgcolor="white">

  <?php
$a=0;
$b=0;

for($i=0;$i<=5;$i++)
{
    $a+=10;
    $b+=5;
}

echo"at end of the loop a= $a and b=$b</br>";
 


$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value </br>";
}


function myname($name)
{
    echo "your name is".$name."</br>";    

}

echo "Hi!";
myname("james");


function mul($x,$y)
{
    $total=$x*$y;
    return $total;

}

echo "the value of=".mul(7,3);
echo "</br>";
function add_five(&$value) {
    $value += 5;
  }
  
  $num = 2;
  add_five($num);
  echo $num;
  
  echo "</br>";

  function hello()
  {

    echo "hi";

  }

  $check="hello";

  $check();

  $sub=array("wd","ADA");
  echo $sub[0]."and" .$sub[1]."are good subjects";


  $char=array(name=>"suresh",age=>22);
    echo $char[age];

    $char1=array(
          array(firstname=>"ravi",lastname=>"patel"),
          array(firstname=>"mayur",lastname=>"xyz")
        );

        //echo $char1[firstname];

        foreach($char1 as $val)
        {
            foreach($val as $key=>$final_val)
            {
                echo "$key:$final_val</br>";

            }
        }

        $cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
echo"</br>";

$check=array("free","program","apple");
sort($check);
echo $check;
//print($check);

foreach($check as $var)
{
    echo "$var</br>";

}

//array_slice()

$first=array("a","b","c","d","e");
$second=array_slice($first,1,2);
foreach($second as $var)
{
    echo "$var</br>";

}

//array_merge()

$first=array("a","b","c","d","e");
$second=array(1,2,3);
$third=array_merge($first,$second);
foreach($third as $var)
{
    echo "$var";

}

?>
</body>
</html>


 Example-6

<html>
<body>
<?php
if(isset($_POST["submit"]))
{
$value = $_POST['val_1'];
}
else
{
$value = "";
}
?>
<form method="POST" action="">
Enter your age <br>
<input type="text" name="val_1" value="<?php echo $value; ?>"> <br />
<input type="submit" name="submit" value= "click here">
</form>
<?php
if($value)
{
if($value>=18)
{ echo "You are Eligible for Vote"; }
else
{ echo "You are not Eligible for Vote"; }
}
?>
</body>
</html>


 Example-7

<html>
<head>
<title>STAR</title>
</head>
<body>
<h2>PYRAMIND</h2>
<?php
if(isset($_POST[“Submit”]))
{
$val = $_POST[“val”];
$symbol = $_POST[“symbol”];
}
else if(isset($_POST[“clear”]))
{
$val = “”;
$symbol = “”;
}
else
{
$val = “”;
$symbol = “”;
}?>
<form method=”POST”>
Enter the loop Number <input type=”text” name=”val” value="<?php echo”$val”; ?>" />
</br>
Enter the Symbol <input type=”text” name=”symbol” value="<?php echo”$symbol”; ?>"/>
</br>
<input type=”submit” name=”Submit”>
<input type=”submit” name=”clear” value=”Reset” >

</form>
<?php
if($val and $symbol)
{
if(is_numeric($val))
{ for($i=1;$i<=$val;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo “&nbsp “.$symbol;
}
echo “<br />”;
}
}

else
{
echo “Enter loop in Digits”;
}
}
?>
</body>
</html>