PHP Basic in just 30 minutes

PHP Basic in just 30 minutes


What is PHP?
PHP is an acronym for "PHP" Hypertext Preprocessor.PHP is a server scripting language and a powerful tool for making dynamic and interactive Web pages. It is one of the most used backend programming languages.
 
  • The file extension of PHP files is .php.
  • PHP code are written between the <?php opening and ?> closing tag.

What can PHP do?
Anything. PHP is mainly focused on server-side scripting, so you can do anything such as collect form data, generate dynamic page content, or send and receive cookies. But PHP can do much more.
There are three main areas where PHP scripts are used.
1.Server-side scripting. This is the most traditional and main target field for PHP.
2.Command-line scripting. You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. about Command line usage of PHP for more information.
3. Writing desktop applications. PHP is probably not the very best language to create a desktop application with a graphical user interface, but if you know PHP very well, and would like to use some advanced PHP features in your client-side applications you can also use PHP-GTK to write such programs. You also have the ability to write cross-platform applications this way. PHP-GTK is an extension to PHP, not available in the main distribution. If you are interested in PHP-GTK, visit » its own website.

What will you need to start?
You need a server to run .php file. In this course, I will use the Apache server. You will get in Xampp tools. It's the best tool for severing dynamic pages using Apache server and also have database facility to store your data like text, images.
If you don't have Xampp install on your PC. Then follow this tutorial on how to install xampp in windows 10.
After installation xampp, open the control panel and active apache server by clicking the active button. And finally, in your machine go to C drive->xampp ->htdocs->
All the PHP files will be in htdocs folder always. So make a folder new and create your first hello.php file within the folder.

Hello world your first program:
test/hello.php
<?PHP

    echo "Hello World!"; //OUTPUT:Hello World!

?>

  • PHP scripts are written always within <?php ?> tag.
  • Each statement ends with a semicolon ;
Print data using echo()/print() Function:
Both echo() /echo and print() /print the function works in the same way.
<?PHP
    // WITH PARENTHESES

    echo("Hello, WELCOME from easylearbd !!!!"); /*OUTPUT:Hello, WELCOME from easylearbd !!!! */
    print("Hello, WELCOME from easylearbd !!!!"); /*OUTPUT:Hello, WELCOME from easylearbd !!!! */

    // WITHOUT PARENTHESES
    echo "Hello, WELCOME from easylearbd !!!!"; /*OUTPUT:Hello, WELCOME from easylearbd !!!! */
    print "Hello, WELCOME from easylearbd !!!!"; /*OUTPUT:Hello, WELCOME from easylearbd !!!! */

    //BOTH WORKS
?>
Comments in PHP:
Comments are the most useful tag. Programmers use to briefly describe his/her code for other programmers. In PHP //bla bla use to single-line comment and /* bla bla bla */ for multiline comments.
<?PHP

    echo("Hello learners , WELCOME from easylearbd !!!!"); /*ECHO FUNCTION WITH PARENTHESES . */

    //THIS IS A SINGLE LINE COMMENT

    /* THIS IS A MULTILINE COMMENT. HERE YOU CAN ADD MORE LINE TO BREIFLY DESCRIBE YOUR CODE. PHP DOEST EXECUTE THIS PART OF CODE ANYMORE. */

?>
Variables in PHP:
Variable is something that stores your data. It can be a string or can be a number.
<?PHP

    $name = "Jone Doe"; // name is a string variable
    $age = 35; // age is also a variable but it is an integer variable

    echo "I am ".$name ." I am ".$age . " years old." /*OUTPUT:I am Jone Doe I am 35 years old. */

?>
Data Types in PHP:
PHP supports the following data types.
  • String
  • Integer
  • Float (floating-point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource
String formatting:
<?PHP

    $name = "Jone Doe";
    echo "Your name is $name"; // OUTPUT:Your name is Jone Doe

    $name = "Jone Doe";
    echo 'Your name is $name'; // OUTPUT:Your name is $name

    $name = "Jone Doe";
    echo "Your name is ". $name; // OUTPUT:Your name is Jone Doe

    $name = "Jone Doe";
    echo 'Your name is '. $name; // OUTPUT:Your name is Jone Doe

?>
Notice that there have two types of string formation first one is Double-quoted strings and the second one is single-quoted strings.
In single-quoted strings, you can define a string using single-quote only when you want to print exactly the same variable string. Within the single-quote, variable names act like a normal string. On the other hand, you can use double-quote to print strings with Variables. In this type of declaration, each variable will be replaced by its value.
Integer formatting:
<?PHP

    $number = 100;
    echo $number; //OUTPUT: 100

?>
Boolean Datatypes:
<?PHP

    $value1 = 100;
    if($value1 === 100){
        echo "Execute this block because if block return true";
    }else{
        echo "this block wont be executed!";
    }

    echo "\n";

    $value2 = 100;
    if($value2 === 50){
       echo "this block wont be executed!";
    }else{
        echo "Execute this block because else block return true";
    }

?>
True and false are also called Boolean operators. In the first example, if block returns true because of the $value1 the same as giving value(100). And the second example, else block will be executed because first if statements give false as a result the else block returns true. and this is why second block printed.
Constants in PHP:
A constant is an identifier for a simple value. The value cannot be changed during execution. define() is used to create a constant.
Syntax: define(const_name,const_value,boolean) . Default value is false for boolean as a result the constant name is case-sensitive. If you set boolean true, the constant name is not case-sensitive.
<?PHP

    // case-sensitive constant name
    define("NAME", "Jone Done");

    echo NAME; //OUTPUT:Jone Done

    echo NAMe; //OUTPUT:NAMe

    define("NAME", "Jone Done",true);

    echo name; //OUTPUT:Jone Done

?>
Conditional statements in PHP:
It is something that checks some statements with respect to a certain value.
<?PHP

    $your_age = 28; /*Declare a variable name $your_age that holds integer value 28. */

    if($your_age>0 && $your_age<10){ // The following condition are checking with respect 28
        echo "I considered you are a baby";
    } else if($your_age >=10 && $your_age<=18){
        echo "You are a teenage";
    } else if($your_age>18 && $your_age<=35){
        echo "You are young"; // got it this statement will be executed and print out
    } else{
       echo "you are a old man";
    }

    //OUTPUT:You are young

?>
Operators in PHP:
Operators are used to performing operations on variables and values.
In PHP, there are following operators are used to perform different operations.
  • Arithmetic operators
  • Assignment operators
  • Increment/Decrement operators
  • Comparison operators
  • Logical operators
  • String operators
Arithmetic operators: We can simply use the arithmetic operator to perform Addition, Subtraction, Multiplication, Division.
<?PHP

    $num1 = 100;
    $num2 =20;

    // addition
    echo $num1+$num2; //OUTPUT:120

    //subtraction
    echo $num1-$num2; //OUTPUT:80

    //multiplication
    echo $num1*$num2; //OUTPUT:2000

    //division
    echo $num1/$num2; //OUTPUT:5

?>
Assignment operators: We can use assignment operators with numeric values to write the number in a variable the following way:
<?PHP

    $x = 100;
    $x2 = $x;
    echo $x2; //OUTPUT:100

    $m += $x2; // $m = $m + $x2; $m=0
    echo $m; // OUTPUT:100 ;

    $m=0+100 $m -= 10; //$m = $m-10 ;$m=100-10=90
    echo $m;

    $n=5;
    $m *= $n; //$m=$m*$n
    echo $m; //$m=90*5=450
    $m /= $n; //$m = $m/$n
    echo $m; //$m = 450/5 = 90

?>
Increment/Decrement Operators: Increment operator is used to increase a value and decrement operator is used to decrease value.
  • ++$variable ; pre-increment
  • $variable++ ;post-increment
  • --$variable; pre-decrement
  • $variable--;post-decrement
<?php

$value = 10; echo ++$value; //OUTPUT:11;increment one and add to 10 makes 11

$value1 = 10; echo $value1++;//OUTPUT:10; increment one after printing 10

$value2 = 10; echo --$value2;//OUTPUT:9; decrement one and subtract from 10 makes 9

$value3 = 10; echo $value3--;//OUTPUT:10; decrement one after printing 10

?>
Comparison operators: Most common comparison operators are :
  • == equal
  • != not equal
  • < less than
  • > greater than
  • <= less than or equal
  • >= greater than or equal
Logical Operators: And, Or and Not are the logical operators in PHP.
  1. and return true if both are true
  2. or return true if one of them is true
  3. not return true if the statement is false
  4. && return true if both are true the same as 1 no.
  5. || return true if one of them is true or both are true the same as 2 no.
  6. ! return true if the statement is false
String operators: There are two string operators in PHP.
  • . concatenation
  • .= concatenation assignment
Switch in PHP
When we want to execute different actions based on different conditions, we have to use switch().
switch(condition){
    case cons_1: will be executed if
        cons_1 = condition
        break
    case cons_2: will be executed if
        cons_2 = condition
        break
    case cons_3: will be executed if
        cons_3 = condition
        break
    case cons_4: will be executed if
        cons_4 = condition
        break
   -----------
   default: will be executed if
        cons_n is different from condition // n= 1,2,3,4,5..........
}
Loops in PHP:
Loops are the most powerful function of every programming language. In PHP there are several types of loops that exist. given below:
  • while loop
  • do - while loop
  • for loop
  • foreach loop
while loop syntax:
while(condition){
   if the condition is true then
   this block will be executed
}
do - while loop syntax:
do {
code will be executed;
} while (condition is true)
for loop syntax:
for(initial value; condition checker ; increment/decrement ){

 THIS BLOCK WILL BE EXECUTED IF condition checker IS TRUE.

}
foreach loop syntax: if we want to iterate an array foreach loop is best option. foreach loop is only used in array().
foreach($array as $value){
 will be executed

}
Simple array-Data structure in PHP:
The array is a container that can store different data with the same datatype. Every programming language provides an array. It is the simplest data structure. In PHP array() a function is something like that. If we want to store some names in person the array it looks like this $person_array = array("Bobi","Joni","kiran");
There have 3 major arrays in PHP programming language.
  • indexed array
  • associative array
  • multidimensional-array
Indexed array: Thre is two ways to create an indexed array.
First one looks like:
$person_array = array("Bobi","Joni","kiran");
Seconds one looks like: $person_array[0] = "Bobi"; // It's called zero indexing $person_array[1] = "Joni"; $person_array[2] = "kiran";

Array with the keys-Data structure in PHP:
Associative Arrays in PHP looks like key-value pair ex. "key"=>"value"
Syntax in an associative array :
array("key1"=>"value1","key2"=>"value2","key3"=>"value3", etc..)
<?PHP

    $person = array("name"=>"Jone","age"=>30); // Declare a person array with key-value pair

    // print name value using name key from $person array
    echo $person['name']."\n"; // OUTPUT: Jone
    echo $person['age']; // OUTPUT: 35

    // print all keys from the $person array using array_keys() function
    print_r(array_keys($person));

/* OUTPUT:
Array
(
[0] => name
[1] => age
)

*/

    // print all values from $person array using array_values() function

    print_r(array_values($person));

/* OUTPUT:
Array (
[0] => Jone
[1] => 30
)
*/

// checking weather key exists or not in the $person array, if exists then if block will be // execute

    if(array_key_exists("age",$person)){
        echo $person['age'];
    }

?>
Functions in PHP:
We will discuss the PHP functions with examples step by step:
There are two types of functions in PHP first one is Buil-in-functions and it is the power of every programming language. The second one is user-defined-functions means programmers can create his own defined function in his program.
There are a lot means huge more than a thousand build-in-functions in PHP. Given list may be helpful for build-in-functions:

But our Main focus on User-defined-functions: User-defined-functions may be classified into four categories such as:
Functions with NO Arguments and NO Return value:
<?php

// Declare a function name
function printMessage(){
  echo 'Hello learns welcome to my site';
}

// Calling the printMessage function
printMessage();

?>
Functions with NO Arguments and Return value:
<?php

    // Declare a function name
    function printMessage(){
        return 'Hello learns welcome to my site';
    }

    // Calling the function and echo that function
    echo printMessage();

?>
Functions with Arguments and NO Return value:
<?php

    // Declare a function name
    function addTwoNumbers($num1,$num2){
        echo $num1+$num2;
    }

    // Calling the function with arguments
        addTwoNumbers(100,200);

?>
Functions with Arguments and Return value:
<?php

    // Declare a function name
    function addTwoNumbers($num1,$num2){
        return $num1+$num2;
    }

    // Calling the function and echo it
        echo addTwoNumbers(200,200);

?>

Congratulations! You Finished it. Now go ahead!!!
Learn PHP basic is not enough to be a Good programmer or Web Developer. Then you have to learn the Advanced topics of PHP. Our next series will be discussed advanced PHP with MySQL Database. We build some cool awesome projects and will be learning new items regularly. I hope It helps a lot who want to learn Web Development from scratch. 

You may like these posts