PHP - Arithmetic Operations


  • Addition (+)
    Addition is performed with the "+" symbol.
    Ex:
    $total = 5 + 6;
    Here 5 add to 6 and assign to the total variable 
     
  • Subtract (-)
    Subtract is performed with the "-" symbol. 
    Ex:
    $answer = 8 - 6; 
      
  • Multiplication (*)
    Multiplication is performed with the "*" symbol. 
    Ex:
    $answer = 5 * 6; 
     

PHP - Variables and Data types

What is a Variable?

Variables are used to store values into the system's memory. Variables allows to perform set of actions and can be re-use within the code. You can change the out put of the program by changing the variable values rather changing the script.

Data Types

String    : used for character values such as sentences, names stc
Integer  : used to store numeric values of whole numbers
Float      : used to store numeric values of real numbers
Boolean: used to store either true or false or 1 or 0
Object   : used to store collection of values (data) or methods

Important: PHP is loosely typed language, that means your script determines the data type of the variable. 

Ex: 
$val = "PHP Programming" // This is considering as String Value
$val = 50 // This is considering as Integer Value

$val = "100" //This is considering as String value
$val =  $val * 5 // This is considering as Integer value
$val = $val * 1.25 //This is considering as float value

PHP - Embeded HTML

This post desribe about the PHP scripting, html coding and embedded php scripts.

I'll explain those with an example.

<html>
  <head>
    <title>Embedded php</title>
  </head>
  <body>
    <hr />
    <h3>Embedded php</h3>
    <p>This is a regular HTML paragraph</p>
    <?php
      echo "<p>This is a php paragraph</p>";
    ?>
    <hr />
  </body>
</html>

You can see the text inside the php script and out side it. Text inside the php script (comes with echo statement) handled as a script and rest of the text rendered as regular HTML. We can include any no of php blocks inside the html document like java script and any html tag to embedded the code.

Above code displays following  output.

Embedded php

This is a regular HTML paragraph
This is a php paragraph.

PHP - Install a Web Server

  • Wamp Server
    This video instruct you how to install wamp server

  • Xampp server
    This video instruct you how to setup xampp server on linux (Fedora)

PHP - My First PHP Program

This is a basic php embeded HTML program.

<?php
    echo "<h3>Hello This is my First PHP Program </h3>";
?>

<?php
    print ("<h3>Hello This is my First PHP Program </h3>");
?>

According to the above program you can appear Following out put

Hello This is my First PHP Program


  • echo and print statements are use to write something in the browser. Within the echo statement we can use html tags to format the text.

PHP - Comments

There are 02 comments used with PHP scripts.

  1. Single line Comment
    Single line comments can be specified by //
    Ex:

    <?php
    // This script print Hello World in the browser

         echo "Hello World"
    ?>
  2. Multiple line Comment
    Multiple Line Comment begins with "/*" and ends with */.
    Ex:

    <?php 
    /* This is a multiple line comment You can comment
    any no of lines within this block
    */

         echo "Hello World"
    ?>

PHP - How to create php files

Normally PHP script block starts with <?php and ends ?> and it can be embedded with HTML. PHP block can be placed anywhere in the document either with html tags or not.

HTML Embeded php code
<html>
  <head>
    <title>This is my First PHP Program</title>
  </head>
  <body>
    <?php
      echo "This is my First PHP Program";
    ?>
  </body>
</html>

Pure PHP Code

<?php
      echo "This is my First PHP Program";
?>

Shorthand PHP Code

Following code executed when the shorthand mood enabled in the server.

<? echo "This is my First PHP Program"; ?>

It is necessary to specify extention of the php file name as ".php" other wise php scrip does not execute.
Ex: Hello.php