Thursday, 21 November 2024

PHP Basic-1

 Example 1: 

 <!DOCTYPE html>

<html>

  <head>

    <title></title>

  </head>

  <body bgcolor="white">


  <?php

        echo "hello world</br>";

        print "hi!how are you?";

          ?>

</body>

</html>



 Example 2:

<html>

<head>

<script type="text/javascript">

$(document).ready(function(e){

 $('#btnSubmit').click(function(){ 

 var email = $('#txtEmail').val();

 if ($.trim(email).length == 0) {

 alert('Please Enter Valid Email Address');

 return false;

 }

 if (validateEmail(email)) {

 alert('Valid Email Address');

 return false;

 }

 else {

 alert('Invalid Email Address');

 return false;

 }});

});

</script>

</head> <body>

Email Address: <input type="text" id="txtEmail"/><br/>

<input type="submit" id="btnSubmit" Value="Submit" />

</body>

</html> 


 Example 3:

<html>

<body>

<form name="myform" action="welcome.php" method="post">

Name: <input type="text" name="name"><br>

E-mail: <input type="text" name="email"><br>

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

</form>

</body>

</html>


 Example 4:

<?php

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

{

$num1=$_POST['num1'];

$num2=$_POST['num2'];


$result=$num1+$num2;


echo "Sum of " .$num1. " and ".$num2. " is " .$result;

}

?>

<!DOCTYPE html>

<html>

<head>

<title>Add two number</title>

</head>

<body>

<table>

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

<tr>

<td>Number 1 :</td>

<td><input type="text" name="num1" required></td>

</tr>

<tr>

<td>Number 2 :</td>

<td><input type="text" name="num2" required></td>

</tr>

<tr>

<td></td>

<td><input type="submit" value="Add" name="submit" /></td>

</tr>

</form>

</table>

</body>

</html>


  Example 5:

<!DOCTYPE html>

<html>

  <head>

    <title></title>

  </head>

  <body bgcolor="white">

  <?php

        echo "hello world</br>";

        print "hi!how are you?";

        $text1="hello";

        $text2=10;

        echo $text1;

        echo $text2;

        echo $text1 ." " .$text2;

        echo $text1;

        print($text1);

        $bikes=array("yamaha","royal","honda","hero");

        var_dump($bikes);

        echo  "array element 1:$bikes[0] $bikes[1]$bikes[2] </br>";

        $x = "Hello world!";

        $x = null;

        var_dump($x);

echo "</br>";

      $x = 5985;

var_dump($x);

$cars = array("Volvo","BMW","Toyota");

var_dump($cars);

echo "</br>";

$text="hello world";

$text=null;

echo $text;

var_dump($text);

echo "</br>";

        $text2="xyz\"this is string\"pqr";

        echo $text2;

        echo "</br>";

        echo strlen("php programming");

        echo "</br>";

        echo str_word_count("php programming language"); 

        echo "</br>";

        echo strrev("JAMES"); 

        echo strpos("Hello world!", "world"); 

        echo strpos("developer news","news");

       $x = 5985;

        var_dump(is_int($x));

        $x = 1.9e411;

        var_dump($x);

        $x = acos(8);

        var_dump($x);

        echo "</br>";

// Cast float to int

$x = 23465.768;

$int_cast = (int)$x;

echo $int_cast;

echo "<br>";

// Cast string to int

$x = "23465.768";

$int_cast = (int)$x;

echo $int_cast;

echo "<br>";

echo(pi()); // returns 3.1415926535898

echo "<br>";

echo "<br>";

echo(rand(1000, 10000));

define("cars", [

  "Alfa Romeo",

  "BMW",

  "Toyota"

]);

echo cars[0];

    ?>

</body>

</html>


  Example 6:

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

  <?php

    $a=45;
    $b=35;

    $temp=$a;
    $a=$b;
    $b=$temp;

    echo "after the swapping :</br></br>";
    echo "a=".$a. "b=".$b;

  ?>

  Example 7:

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

  <?php


    $a=45;
    $b=35;
    $c=20;

     
    if($a>$b and $a>$c)
    {
        echo "Max No. is $a";
    }
    elseif($b>$c)
    {
        echo "Max No. is $b";

    }
    else
    {
      echo "Max No. is $c";

    }

   ?>
   </body>
   </html>