Arithmetic Operations, PHP Control Structures, Logical Expressions,php tutorial: 



Arithmetic Operations example code :


<?php
$a=30;
$b=15;
$a =$a+$b; //assigning variable $a to new value 45
echo $a.'this a is after assigning new variable a'.'<br>';
$total1=$a-$b;   //assigning variable $total1 to new value $a-$b and rest of them are same
$total3=$a*$b;
$total4=$a/$b;
$total5=$a%$b;
echo $total1.'<br>'; //showing the results
echo $total3.'<br>';
echo $total4.'<br>';
echo $total5.'<br>';
?> 

PHP Control Structures example code:


<?php 

$foo=10;


if($foo == 0) {
echo "The  foo is equal to 0";
}
else if (($foo > 0) AND ($foo <= 5)) {
echo " The variables foo is between 1 and 5" ;
}
else {
echo "The variable foo is equal to ".$foo;
}

?>

Logical Expressions example code:



<html>
<head>
<title>Logical Expressions</title>
</head>
<body>A
<?php
$a = 5;
$b = 5;
if ($a > $b) {
echo "a is larger than b";
} elseif ($a == $b) { // if 1st expression is false, elseif performs a 2nd test
echo "a equals b";
} else { // if all expressions are false, else gives an alternative statement and run it
echo "a is smaller than b";
}
?>
<br />
<?php
$c = 1;
$d = 20;
if (($a > $b) AND ($c > $d)) {// && is a logical AND ,  || is a logical OR
echo "a is larger than b AND ";
echo "c is larger than d";
}
if (($a > $b) OR ($c > $d)) {
echo "a is larger than b OR ";
echo "c is larger than d";
} else {
echo "neither a is larger than b or c is larger than d";
}
?>
<br />
<?php
// ! is a logical NOT and it negates an expression
unset($a);
if (!isset($a)) { // read this as "if not isset $a" 
$a = 100;
}
echo $a;
?>
<br />
<?php
// Use a logical test to determine if a type should be set
if (is_int($a)) {
settype($a, "string");
}
echo gettype($a);
?>
</body>
</html>

    what is PHP?

    PHP is a server side scripting language. Now you may have thought that PHP was a
    programming language. Well, technically speaking, it's not.
    So how's a scripting language different from a programming language?
    The distinction between them is largely artificial, and the lines can get a bit blurry.
    But we can do a general comparison. A script only runs in response to an event.
    It also usually runs a set of instructions by working down the page
    from the start to the end. It has little or no user interaction
    after that initial event. So PHP script does not run until a web
    page is requested. Then it launches, follows its
    instructions from top to bottom, and then quits until another action launches the
    script again. On the other hand, a program, runs even
    when not responding to events. It continues to run, and to wait for interaction.
    Whether that interaction comes from a user, making choices, or from other
    programs or input. A programs also jumps around with
    instructions a lot more. So that there's often not a clear start
    and end point. And it often involves lots of user interaction.
    Photoshop is a good example of an application.
    After you launch it, it keeps running, waiting for more interactions or for you
    to tell it to quit. The task that it performs are not a
    linear set of instructions. It jumps around based on the task that
    you want to do at that particular moment. But as I said, the lines get blurry as
    scripts get more complex, they start to resemble programs.
    And the simplest programs are basically just scripts.
    So you could say it's a distinction without a difference.

    But we still call PHP a scripting language.
    Now what does it mean when we say PHP is server side.
    When we talk about server side and its opposite, client side.
    What we're talking about is where the code does its work.
    Does the code run on our web server, which is server-side, or on the user's
    computer, which is client-side? The client-side, when we're working with
    web pages, is the user's browser. As a contrast, JavaScript is an example
    of another popular scripting language, but JavaScript is a client-side scripting language.
    JavaScript code is sent to the user's browser and then it does its work there.
    PHP code is never sent to the user. It runs entirely on the web server, and
    the results of that code is what's sent to the user's browser.
    That's an important difference. Because PHP runs on a web server, that
    means it generally can't run on its own. We'll need to have a running web server
    in order to use PHP. PHP code does not need to be compiled.
    It's executed by the web server exactly as it's written.
    Other programming languages, such as C or Java, require the code to be compiled or
    translated into another form before it can be used.
    We'll be able to just write our PHP, put it where our web server can find it, and
    then we can load up the web page and see the results.
    PHP is designed for use with HTML. It can be embedded in our HTML.
    And we can use it to generate HTML. In the end PHP is going to return HTML to
    the browser. PHP code is going to be our input and web

    pages are going to be our output. Now if you've been working with HTML
    you're already familiar with having .htm or .html at the end of your file names.
    PHP is going to work exactly the same but we're going to put php at the end.
    The php is going to tell the web server that this file contains php codes that
    needs to be executed. PHP is going to provide more flexibility
    than html does on it own. HTML pages are static by their nature.
    So all visitors to a web page see that same page all the time.
    A PHP lets us create dynamic pages. And page content can change based on conditions.
    Such as interactions with the user, or data stored in a database.
    You can think of PHP as turbo charging your html.
    PHP syntax is going to be very similar to C, Java, and Perl.
    The small details are going to vary quite a bit, but the structure of logical
    expressions and loops, those kinds of things will be kind of familiar to anyone
    with programming experience in one of these languages.
   
In this blog I'll be discussing you about php variable,logical expression, operators, 

Example code for Loops pointers in php :


<html>
    <head>
        <title>Loops: pointers</title>
    </head>
    <body>
    <?php
   
        $ages = array(43, 84, 154, 146, 213, 142);
    ?>
    <?php
       
        echo "1: " . current($ages) . "<br />";
        next($ages);
        echo "2: " . current($ages) . "<br />";
        reset($ages);
        echo "3: " . current($ages) . "<br />";
    ?>
    <br />
    <?php
       
        while ($age = current($ages)) {
            echo $age . ", ";
            next($ages);
        }
    ?>
    </body>
</html>

Example code of functions in php

 


<html>
    <head>
        <title>Functions2</title>
    </head>
    <body>
    <?php
      $bar = "outside";
    function foo() {
        global $bar;
        $bar = "inside";
    }
    foo();
      echo $bar . "<br />";

    ?>
    <br />
    <?php
    // Example using a local variable, arguments and return values
    $bar = "outside";
    function foo2($var) {
        $var = "inside";
        return $var;
    }
    $bar = foo2($bar);
    echo $bar . "<br />";

       ?>
    <br />
    <?php    
        function paint($color="red", $room="office") {
            echo "The color of the {$room} is {$color}.";
        }
        paint("blue","bedroom");
    ?>
   
   
   
    </body>
</html>

 Example of Functions in php:

 

<html>
    <head>
        <title>Functions</title>
    </head>
    <body>
       <?php
        // a simple function example
        function say_hello() {
            echo "Hello World!<br />";
        }
        say_hello();

        // a function with 1 argument
        function say_hello2($word) {
            echo "Hello {$word}!<br />";
        }
        say_hello2("World");
     
                say_hello2("Everyone");

    ?>

    <?php
              $name = "John Doe";
        say_hello2($name);
    ?>
   
    <?php
        // function with multiple arguments
        function say_hello3($greeting, $target, $punct) {
            echo $greeting . ", " . $target . $punct . "<br />";
        }
        say_hello3("Greetings", $name, "!");
    ?>
    <br />
    <?php
               function addition($val1, $val2) {
            $sum = $val1 + $val2;
            return $sum;
        }
       
             $new_val = addition(3,4);
        echo $new_val;
       
               if (addition(5,6) == 11) {
            echo "Yes";
        }

            
             function add_subt($val1, $val2) {
            $add = $val1 + $val2;
            $subt = $val1 - $val2;
            $result = array($add, $subt);
            return $result;
        }
        $result_array = add_subt(10,5);
        echo "Add: " . $result_array[0];
        echo "Subt: " . $result_array[1];

    ?>
   
   
   
    </body>
</html>

Example of foreach loop in php



<html>
    <head>
        <title>Loops: foreach</title>
    </head>
    <body>
   
    <?php
        $ages = array(43, 83, 125, 126, 223, 242);
    ?>

    <?php
        // using each value
        foreach($ages as $age) {
            echo $age . ", ";
        }
    ?>
    <br />
    <?php
        // using each key => value pair
        foreach($ages as $position => $age) {
            echo $position . ": " . $age . "<br />";
        }
    ?>
    <br />
    <?php
             $prices = array("Brand New Computer"=>2000,
        "1 month in "=>25,
        "Learning PHP" => "priceless");
        foreach($prices as $key => $value) {
            if (is_int($value)) {
                echo $key . ": $" . $value . "<br />";
            } else {
                echo $key . ": " . $value . "<br />";
            }
        }
    ?>


    </body>
</html>

Example of for loop in php :


<html>
    <head>
        <title>Loops: for</title>
    </head>
    <body>
   
    <?php
       
        for ($count=0; $count <= 10; $count++) {
            echo $count . ", ";
        }
    ?>
    </body>
</html>
<html>
    <head>
        <title>Floating Point Numbers</title>
    </head>
    <body>
        <?php
                       $var1 = 3.14
        ?>
        <?php
                      echo 4/3;
        ?>
        Floor: <?php echo floor($myFloat); ?><br />
        Floating point: <?php echo $myFloat = 3.14; ?><br />
        Round: <?php echo round($myFloat, 1); ?><br />
        Ceiling: <?php echo ceil($myFloat); ?><br />
      
       
    </body>
</html>

 Example code for Loops  continue in php

<html>
    <head>
        <title>Loops  continue</title>
    </head>
    <body>


    <?php
       
        for ($count=0; $count <= 10; $count++) {
            if ($count == 5) {
                continue;
            }
            echo $count . ", ";
        }
    ?>
    </body>
</html>

 Example of Constants in php

<html>
    <head>
        <title>Constants</title>
    </head>
    <body>
        <?php
           
           
            $max_width = 980;
           
            define("MAX_WIDTH", 980);
           
           
            echo MAX_WIDTH; echo "<br />";
           
           
           
           
            $max_width += 1;
            echo $max_width;
           

        ?>
    </body>
</html>

Code of loop break in php:



<html>
    <head>
        <title>Loops  break</title>
    </head>
    <body>
    <?php
       
        for ($count=0; $count <= 10; $count++) {
            echo $count;
            if ($count == 10) { break; }
            echo ", ";
        }
    ?>

    </body>
</html>
<html>
    <head>
        <title>Array Functions</title>
    </head>
    <body>
        <?php $array1 = array(5,2,144,15,33,52); ?>

        Count: <?php echo count($array1); ?><br />
        Max value: <?php echo max($array1); ?><br />
        Min value: <?php echo min($array1); ?><br />
        <br />
        Sort: <?php sort($array1); print_r($array1); ?><br />
        Reverse Sort: <?php rsort($array1); print_r($array1); ?><br />
        <br />
        <?php
           
        ?>
        Implode: <?php echo $string1 = implode(" * ", $array1); ?><br />
        Explode: <?php print_r(explode(" * ", $string1)); ?><br />
        <br />
        In array: <?php echo in_array(15, $array1); // returns T/F ?><br />
       
    </body>
</html>

Create a temperature converter in php :

Here is the code for temperature converter in php 

 



<?php
    function fahrenheit_to_celsius($given_value)
    {
        $celsius=5/9*($given_value-32);
        return $celsius ;
    }

    function celsius_to_fahrenheit($given_value)
    {
        $fahrenheit=$given_value*9/5+32;
        return $fahrenheit ;
    }

?>

<html>
    <head>
        <title>Temp. Conv.</title>
    </head>
    <body>
        <form action="" method="post">
        <table>
          
            <tr>
                <td>
                    <select name="first_temp_type_name">
                        <option value="fahrenheit">Fahrenheit</option>
                        <option value="celsius">Celsius</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="given_value">
                </td>
            </tr>
            <tr>
                <td>
                    <select name="second_temp_type_name">
                        <option value="fahrenheit">Fahrenheit</option>
                        <option value="celsius">Celsius</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="btn" value="Convert">
                </td>
            </tr>
            <tr>
                <td>
                    <?php
                    if(isset($_POST['btn']))
                    {
                        $first_temp_type_name=$_POST['first_temp_type_name'];
                        $second_temp_type_name=$_POST['second_temp_type_name'];
                        $given_value=$_POST['given_value'];
                        if($first_temp_type_name=='fahrenheit')
                        {
                            $celsious=fahrenheit_to_celsius($given_value);
                            echo "Fahrenheit $given_value = $celsious Celsious";
                        }
                        if($first_temp_type_name=='celsius')
                        {
                            $fahrenheit=celsius_to_fahrenheit($given_value);
                            echo "Celsious  $given_value = $fahrenheit Fahrenheit";
                        }
                       
                       
                    }
                   
                    ?>
                </td>
            </tr>
        </table>
        </form>
    </body>
</html>

Create a password generator by yourself in php:

Here is the code for Create a password generator :



<?php
  function generate_password($length)
    {
        $alpha=array('q','p','Z','o','R','*','&','@');
        $password='';
        for($i=1;$i<=$length;$i++)
        {
            $rand_value=rand(0,7);
            echo '----'.$rand_value."<br/>";
            $password .=$alpha[$rand_value];
           
        }
        return $password;
    }

?>

<html>
    <head>
        <title>Generate Password</title>
    </head>
    <body>
        <form action="generate_password.php" method="post">
        <table>
          
            <tr>
                <td>
                    <input type="text" name="length">
                </td>
            </tr>
           
            <tr>
                <td>
                    <input type="submit" name="btn" value="Generate password">
                </td>
            </tr>
            <tr>
                <td>
                    <?php
                    if(isset($_POST['btn']))
                    {
                       
                        echo generate_password($_POST['length']);
                    }
                   
                    ?>
                </td>
            </tr>
        </table>
        </form>
    </body>
</html>

Create a simple calculator with php OOP:

Create a file called calculator_view.php . Now paste the following code.



  1. <?php
    require_once './classes/calculator.php';
    $obj= new Calculator();
    ?>


    <html>
        <head>
            <title>Loop Example</title>
        </head>
        <body>
            <form action="calculator_view.php" method="post">
                <table>
                    <tr>
                        <td>First Number </td>
                        <td>
                            <input type="text" name="first_number" value="<?php echo $_POST['first_number'];?>">
                        </td>
                    </tr>
                    <tr>
                        <td>Ending Number </td>
                        <td>
                            <input type="text" name="second_number" value="<?php echo $_POST['second_number'];?>">
                        </td>
                    </tr>
                   
                    <tr>
                        <td>&nbsp;</td>
                        <td>
                            <input type="submit" name="btn" value="+">
                            <input type="submit" name="btn" value="-">
                            <input type="submit" name="btn" value="*">
                            <input type="submit" name="btn" value="/">
                            <input type="submit" name="btn" value="%">
                           
                        </td>
                    </tr>
                    <?php
                            if(isset($_POST['btn']))
                            {
                                $first_number=$_POST['first_number'];
                                $second_number=$_POST['second_number'];
                               
                                if($_POST['btn']=='+')
                                {
                                    $result= $obj->add($first_number,$second_number);
                                }
                                if($_POST['btn']=='-')
                                {
                                    $result= $obj->sub($first_number,$second_number);
                                }
                                if($_POST['btn']=='*')
                                {
                                    $result= $obj->mul($first_number,$second_number);
                                }
                                if($_POST['btn']=='/')
                                {
                                    $result= $obj->div($first_number,$second_number);
                                }
                                if($_POST['btn']=='%')
                                {
                                    $result= $obj->rim($first_number,$second_number);
                                }
                               
                            }
                           
                           
                           
                            ?>
                    <tr>
                        <td><?php echo $result['lbl'];?></td>
                        <td>
                            <?php echo $result['value'];?>
                        </td>
                    </tr>
                </table>
            </form>
        </body>
    </html>


Now create a folder called classes ,and create a file within the classes folder called calculator.php and paste the following code.


<?php
class Calculator{
   function  add($first_number,$second_number)
    {   $result=array();
        $result['value']=$first_number+$second_number;
        $result['lbl']='Addition';
        return $result;
    }
  function  sub($first_number,$second_number)
    {
        $result=array();
        $result['value']=$first_number-$second_number;
        $result['lbl']='Subtruction';
        return $result;
    }
    function  mul($first_number,$second_number)
    {
        $result=array();
        $result['value']=$first_number*$second_number;
        $result['lbl']='Multiplication';
        return $result;
    }
    function  div($first_number,$second_number)
    {
        $result=array();

        if($second_number==0)
        {
            $result='Undefine';
            $result['lbl']='Divition';
            return $result;
        }
        else{
        $result=array();
        $result['value']=$first_number/$second_number;
        $result['lbl']='Divition';
        return $result;
        }
    }
  function  rim($first_number,$second_number)
    {
        $result=array();
        if($second_number==0)
        {
            $result='Undefine';
            $result['lbl']='Reminder';
            return $result;
        }
        else{
        $result['value']=$first_number%$second_number;
        $result['lbl']='Reminder';
        return $result;
        }
    }
}

?>
Subscribe to RSS Feed Follow me on Twitter!