Saturday, 6 August 2016

PHP Introduction - Lecture #7

PHP LOOPS Contd.

Do While Loop

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
Today our Topic is

Do-while LOOP

The simple difference between while and do-while loop is that while loop checks the condition then it at least once executes the code but it do-while loop the block of code is executed then condition is checked. Even if the condition is false then also code will be executed at least once. Here is the simple syntax of do-while loop.
do
  {
  code to be executed;
 
}
while (condition);
Let’s have an example of do-while loop


From the above figure the result comes 2-10 even in last when it comes to 10 the condition becomes false despite of false condition it prints out to 10. Actually that’s the only difference between while and do-while that even condition becomes false at least one time code will be executed.
Description of above code
First of all we have declared a variable named `a` and assigned value of 1 after that we are saying in our code to `do` the increment by 1 in the above value of `a`, it becomes 2 then there is echo command that prints what the value is in variable `a` and it is 2 because before printing we are doing increment so it becomes 2. After that condition is checked either $a is less than 10 or not, it’s true so again it goes back to the `do` command and says to do increment by 1 and becomes 3 then it prints 3 after that it checks condition and so on.
You keep trying yourself with different conditions and examples. Hope you will get the concept.


Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `For Loop`.
Good Luck J

PHP Introduction - Lecture #6

PHP - LOOPS

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
Today our Topic is

LOOPS

The term `loop` is defined as “Repetition of anything”. Here in PHP we use loops when we have to print something multiple times by writing them just single time within a loop.
In almost every programming language there are 4 types of loops.
1-    While Loop
2-    Do-while Loop
3-    For Loop
4-    For each Loop
The last one For each loop will be studied in next coming topic `Arrays` but here we will focus of first three types of loops.
i-                   While loop: This loop works when the specified condition defined in loop syntax is true.
Here is simple syntax
            while( condition )
{
            Code you wants to execute
}
The code within the curly braces will be executed if the condition defined in the above brackets is true. Let’s take an example of first while loop.
First start the php


<?php
            $a = 1;
            while($a < 10){
            echo $a;
            $a++;
}
?>
Description of above code:
First of all we have declared a variable named `a` and assigned value of 1
After then while loop is started with `while` keyword and inside the brackets there is condition defined that $a declared above is less than 10 or not if it’s less than 10 means condition is true if condition is true then remaining part of code will be executed. After while condition is true there is echo statement that prints $a variable and $a will be printed unless and until while condition becomes false. And in last $a++ this is called an increment operator. Increment operator increases the value by adding +1 in the defined value. So variable a will also increase by means of +1.
Here is the code and result of it



The result is up to 9 because if it comes to 10 and look at the condition that says $a < 10 when it becomes 10 then 10 < 10 this condition becomes false and the code execution is stopped. While loop executes the code until condition is true. In simple while loop first checks the condition and then executes the code. Similarly you can print multiple statements using while loop in `echo` statement by creating your own login.

Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `do-while Loop`.
Good Luck J

PHP Introduction - Lecture #5

PHP - Conditions

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
Today our Topic is

Conditional Statements

Conditional Statements are set of commands used to perform different actions based on different conditions. These are used to control flow of code by applying different types of conditions. In PHP we have following conditional statements.
i-                   If
ii-                 Else
iii-               Else if
iv-               Switch
i-                   IF: We use this condition only when a specified condition is true. If the condition is true then do that thing. Here is simple syntax.

If (condition) { // start of if body
      execute the statement
} // end of if body
Let’s take an example
<?php
      $num1 = 10;
      $num2 = 20;
                  if( $num1 < $num2 ){
                  echo “10 is less than 20”;
}
?>
ð If you want to print multiple statements inside the if condition then you can also echo another statement within the if body.



<?Php
$num1 = 10;
      $num2 = 20;
                  if( $num1 < $num2 ){
                  echo “10 is less than 20”;
                  echo “<br />”;
                  echo “20 is greater than 10”;
}
?>
ð `<br />’ will bring the next echo statement in new line.
ð So try to use <br /> if you are printing multiple statements

ii-                 Else: If you want to execute some code if condition is true and another code if the condition is false then you must use else statement.
Let’s take above example and make simple changing in condition.
<?Php
$num1 = 10;
      $num2 = 20;
                  if( $num1 > $num2 ){
                  echo “10 is less than 20”;
                  echo “<br />”;
                  echo “20 is greater than 10”;
}
else{
echo “10 is less than 20”;
}
?>


In condition we are saying that if 10 is greater than 20 then print these statements but the condition is false, so this block/part of code will be skipped and will be moved to else statement and will be printed this else statement. Let’s look for another example.
<?php
$num = 15;
If($num >= 10 && $num <= 20){
echo “the number is between 10 and 20”;
}
else{
echo “number is something else”;
}
?>
In above example we have checked two conditions using AND (&&) Operator that either the number is greater than 10 or less than 20 then print if statement otherwise print else statement.
iii-               Else-If: This condition is used when we know that there would be any one out of the multiple statements is true, rest of the others are false. Else if is known as the extension of if structure. If one condition fails then it executes another `if` condition.
Let’s look for an example of simple Grading System
<?php
            $per = 87;
            if( $per >= 90 && $per < 100 ){
                        echo "You got A+ Grade";
            }
            elseif($per < 90 && $per >=80){
                        echo "You got A grade";
            }
            elseif($per < 80 && $per >=70){
                        echo "You got B grade";
            }
            elseif($per < 70 && $per >=60){
                        echo "You got C grade";
            }
            elseif($per < 60 && $per >=50){
                        echo "Improve";
            }
            else{
                        echo "You are fail";
            }          
?>
Below is the code and result of else if example.


In last it’s good to include else statement if any of the above conditions are false then else will be executed.
i-                   Switch: Switch statement is used to compare a variable and/or an expression to different values. This works like if-else statement there is no any main difference between if-else statement and switch statement the only difference is of syntax.
Here is the syntax

switch ( <variable> ) {
            case this-value:
                        Code to execute if <variable> == this-value
                        break;
            case that-value:
                        Code to execute if <variable> == that-value
                        break;...
            default:
                        Code to execute if <variable> does not equal the value following any of the cases
break;
}
First we have an expression or variable, value of that expression or variable is compared with the values of each case in syntax. If there is match in any of case only that block of case is executed. Break keyword is used to prevent the code running into the next case automatically. And if there is no match with any of the case values then default case is executed just like else statement in `elseif` condition. Let’s look for an example.
<?php
            $x = 20;
            switch($x){
            case 10:
            echo “number is 10”;
            break;
case 20:
echo “number is 20”;
break;
case 30:
echo “Number is 30”
break;
default:
echo “unknown number”;
}
?>
If no match is found from any of the cases then default condition will be executed. It exactly works like the `elseif` condition. Try to do Grading System in Switch statement. And further more if you got all the conditional statements try to make Mark sheet Generator System using if, elseif, or even switch statement.

Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `LOOPS`.
Good Luck J

PHP Introduction - Lecture #4

Operators, Concatenation, Comments

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
Today our Topic is

OPERATORS

There are four types of operators
1-    Arithmetic Operator
a.     Addition (+)
b.     Subtraction (-)
c.      Multiplication (*)
d.     Division (/)
e.     Modulus (%)
2-    Assignment Operator
a.     =
b.     +=
c.      -=
d.     *=
e.     /=
f.       .=
g.     %=
3-    Comparison Operator
a.     is equal to                 (==)
b.     is not equal to         (!=)
c.      is not equal to         (<>)
d.     is greater than         (>)
e.     is less than                (<)
f.       is greater than or equal to           (>=)
g.     is less than or equal to                  (<=)
4-    Logical Operator
a.     AND   ( && )
b.     OR      ( || )
c.      NOT    ( ! )

Increment/decrement Operators

Increment operator ++
            Increment operator adds +1 to the given value
Decrement operator –
            Decrement operator subtracts the -1 from the given value.
Example:
<?php
            $a = 10;
            $a++;
            echo $a;
// the result will be 11. Increment operator adds 1 to the variable.
            $b = 5;
            $b--;
            echo $b;
// the result will be 4. Decrement operator subtracts -1 from the variable;
?>

Concatenation

Concatination means to combine something. Here in almost each programming language we use concatenation to combine two parts or two statements as to result as a single statement. Here is an example:
<?php
            $var1 = “hello this is an example”;
            $var2 = “of concatenation”;
            echo $var1 . $var2;
// this will result as `hello this is an exampleof concatenation`
?>
to give space between exampleof words just apply space at the end of first variable or from the first of second variable, within quotations.

Comments

Comments are used by developers in order to know about his code that for what purpose this code is about. Comments are also very useful for the end users if he/she will see the comments besides the code he/she will surely know that what you have written in your code, and what’s logic developer has applied in it. Comments will not affect your code even if you write between your code just place double forward slash `//` before you start writing comment or another way is to write this `/* */` between these you must write comment whatever you want to write in your comment.
// comment is known as Single line Comment
/*   is known as multiline comment.
Whatever we will write between this will not affect our */
It’s very good approach to use comments for every important part of code.

Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `Conditions`.

Good Luck J

PHP Introduction - Lecture #3

PHP - Variables

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
Today our Topic is
`PHP Variables`.
As if you concentrate on the topic it’s PHP Variable. A Variable means something that is changeable. PHP variable is termed as something that stores value or holds information is known as Variable, why its name is Variable because its value or information does not remains same it can be changed at any time. We can say variable is just like a Container of memory that holds some information in it. The main purpose of declaring a variable that we store our information in a variable and when needed we just simply call that variable.
There are three types of variables
i-                   Local Variables: variable that is accessed from just single page
ii-                 Global Variables: variable that can be accessed from multi pages
iii-               Session Variables: variable that can be accessed from the whole domain.
Here we will discuss just local variables. Rest of others will be discussed in next topics

Declaring a Variable
We can declare multiple variables, with multiple type of information i.e. any character, full sentence, digits, special characters, and also alphanumeric information. But remember the name of each variable must be different otherwise the information we have stored can mix up. Variable names are case sensitive.
Let’s see how to declare a variable. The same process first start PHP tag and close it and start coding inside of these.
            `$` dollar sign is used to initialize a variable.  $ sign with any word is known as a variable. And give this variable a name. let suppose a
            <?php
$a = 10;
            ?>
Above code shows that variable with name a holds the information/value of number 10. In last ( ; sign ) known as terminator means here the value is ended. As it’s important to give this terminator sign at the end of value of the variable. You can declare variable with following formats.
            Formats for declaring a variable
$variableName     (correct)  
$variable_Name    (correct)  
$variable1             (correct)
$variable_2           (correct)  
Remember you cannot declare a variable with a name that starts with a number
$1variable             (wrong)   

Information Holds a Variable
$myVariable = “Double quotes work.”;         (correct)               
$ myVariable = ‘Single quotes work too.’;     (correct)               
$ myVariable = “ One or other. ’;                    (wrong)              
Any information inside quotation is known as String variable, all the characters and alphanumeric values placed within single or double quotation.
Any numeric values are called as Integer variable, and these are not placed within quotations.
Even if you place your numeric values within quotation these will be called as String variable not an Integer.

$string = ‘Don’t mix your quotes.’;                  (wrong)               

$string = “Don’t mix your quotes.”;                 (correct)
$string = “He said “that’s fine,” and left.”;       (wrong)
$string = “He said \“that’s fine,\” and left.”;     (correct)

PRACTICE
<?php
            $variable_one = 20;
            $variable_two = 30;
            $result = $variable_one + $variable_two;
            echo $result;
//The result will be 30
?>
Furthermore you practice on variables by adding, subtracting, multiplying dividing different values with each other.

Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `Operators`.
Good Luck J

PHP Introduction - Lecture #2

Installing and Running PHP

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
Today our Topic is
`Installing and Running PHP programs`.
As in previous lecture I told you that to run PHP programs you need any of these Servers WAMP, XAMPP 32 or 64 bit according to your Operating System configuration.
First of all download any one of them and install it, for windows users only because I’m using windows. After completing the installing
FOR WAMP
            If you have installed WAMP server go to your C drive there must be folder named `wamp` inside this folder go to `www`. In this folder you should save all your PHP programs and run them. Without this folder your browser will not be showing your files for running.




FOR XAMPP
            If you have installed xampp on your server similarly in your C drive there would be a folder of `xampp` inside this folder go to `htdocs`. Here you must save all of your PHP programs for running purpose.
Remember if you save your files without those folders in wamp and xampp your PHP programs will not be executed at the browser.

After you have finished installing any of these servers open any text editor just simple you ca use notepad or notepad++. And here we go for running our first PHP program.
The syntax of any PHP programs starts with `<?php` and end with `?>`. Whatever we write between these will be considered as part of PHP code. Giving this `<?php` at the starting of any code means we are telling our browser that it’s a PHP code.
Simple PHP syntax
<?php
            echo “hello world”;
?>
The term `echo` used to show what we write in the quotation on the browser. Whatever we will write in echo statement will be shown in the browser. From above code the browser will show just `hello world` not rest of other coding.
You write this code in your editor notepad or notepad++ and save it with .php extension in the C drive of `www` or `htdocs` of folder you are told above.
After Saving this file Open your browser and write `localhost` in your URL as shown in picture below:



At the top URL there is `localhost` written, you just ignore `:8181` this is my own configuration you only write the `localhost` and for WAMP users this page will be opened and below another arrow shows your files that you saved on your C drive of www folder, these are my projects that are saved in that folder. You just click on that file and browser results the output.
Note: Remember you must open your wamp/xampp server before you run any PHP program otherwise your program will not execute.
Here is how you open your wamp/xampp server.

WAMP USERS


I hope you got to know how to install and run a simple PHP program. If so then try to practice yourself by running multiple programs again and again.

Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `PHP Variables`.
Good Luck J


PHP Introduction - Lecture #1

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
First of all in this our first lecture we have introduction of PHP…

INTORDUCTION OF PHP
Acronym for Hypertext Preprocessor (PHP), these types of acronyms are called as
`Recursive acronym`. PHP is open source, cross-platform, server side,
Scripting and Programming language especially used for creating Dynamic web pages.
Further we will be discussing these terms one by one.
Rasmus Lerdorf was the founder of PHP. He introduced PHP in 1995. Originally the main purpose of PHP was to have Personal Home Page for every one so firstly its acronym was also Personal Home Page (PHP), later on it became PHP as Hypertext Preprocessor. It’s the only largest Web Development language world widely used.
§      Open Source, this term does not mean that we have code visible to the user but we can know how the PHP language is made what the concepts, documentation is used from PHP.net it’s officially website.
§  Cross-Platform, PHP can run on multiple platforms such as Windows, Linux etc.
§        Scripting and Programming Language, Scripting language means it has a particular way of writing the code. Programming language is rule that we tell a computer what and how to perform a task using some kind of syntax and structure.
§       Server side, To run PHP programs on our system we need a server, Such as WAMP stands for Windows, Apache, MySQL, PHP. This is widows based server. XAMPP (X=any of the four Operating Systems Windows, Linux, Mac, Apache, MySQL, PHP, Python also). LAMP (Linux based server Linux, Apache, MySQL, PHP) and so on…
You can freely download any of them server freely depends upon your operating system and install it.
§      Dynamic, means alterable or changeable. We can make changes in our web page at any time from our server.

Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `Installing and Running PHP programs`.

Good Luck J