Showing posts with label lessons. Show all posts
Showing posts with label lessons. Show all posts

10 January 2011

Php For Beginners (Lesson 6)

In this lesson we will be discussing array variables, array variables are just like ordinary variables except that an array variable can store multiple values in one single variable.

For example if you want to store names of 5 students then you need to have 5 variables. What if students total exceeds 500? Then you will need 500+ variables to store those names. In such situations we will use a single array variable to store the names of all the students.

Array variables have indexes associated with each value, so that the values can be easily accessed using their respective indexes. These indexes can be numeric as well as strings.

Numeric Index Array

Example
<?php 
$student_name[0]=”Bill”; 
$student_name[1]=”Max”; 
$student_name[2]=”John”; 
$student_name[3]=”Smith”; 
echo $student_name[0].” And ”.$student_name[3].” Are good students”; 
?>
The output of above example will be “Bill and Smith Are good students”.

String Index Array

Example
<?php 
$marks[‘bill’]=”96”; 
$marks[‘max’]=”32”; 
$marks[‘john’]=”54”; 
$marks[‘smith’]=”78”; 
echo “Bill secured ”.$marks[‘bill’].” marks”; 
?>
The output of above example will be “Bill secured 96 marks”.

Multidimensional Array

In multidimensional array each element of the main array can also be an array and each element in that array may be an array too, and so on.

Example
<?php 
$students=Array
(
[‘Bill’] => Array
(
[0] => 78
[1] => 86
[2] => 92
)
[‘Max’] => Array
(
[0] => 45
)
[‘John’] => Array
(
[0] => 76
[1] => 63
[2] => 78
)
); 
echo “Bill secured ”.$students[‘Bill’][2].” marks”; 
?>
The output of the above example will be “Bill secured 92 marks”.

References

http://www.php.net
http://www.w3schools.com

I guess we have discussed all the fundamental concepts of array variables, in case of any queries please feel free to comment on this post.


next Previous Lesson --- Next Lesson next

09 January 2011

Php For Beginners (Lesson 5)

In this lesson we will be discussing conditional statements. Conditional statements are used when you want to relate an action with some condition. Php supports four types of conditional statements:

If Statement

Syntax

if (condition)
{ code to be executed }

Example
<?php
$a=5;
if ($a==5)
{
   echo “five”; // outputs “five”
} 
?>
If – else Statement

Syntax

if (condition)
{ code to be executed }
else { code to be executed }

Example
<?php
$a=5;
if ($a==5)
{
   echo “five”; // outputs “five”
}else
{ 
   echo “not five”;
}
?>
If – elseif – else Statement

Syntax

if (condition)
{ code to be executed }
elseif (condition)
   { code to be executed }
else { code to be executed }

Example
<?php
$a=2;
if ($a==5)
{
   echo “five”; 
}elseif ($a==6)
{
   echo “six”; 
}
else 
{ 
   echo “neither five nor six”;  // outputs “neither five nor six”
}
?>
Switch Statement

Syntax

switch (variable)
{
case test1:
  code to be executed if variable =test1;
  break;
case test2:
  code to be executed if variable =test2;
  break;
default:
  code to be executed if n is different from both test1 and test2;
} 

Example
<?php
$a=2;
switch ($a)
{
case 5:
  echo “five”;
  break;
case 2:
  echo “two”; // outputs “two”
  break;
default:
  echo “neither five nor two”;
}  
?>
The switch statement takes variable as an argument and then code to be executed for each case. Whichever condition will be true it will execute its code and if no condition is true then it will execute the default code.

References

http://www.php.net
http://www.w3schools.com

This is all for the fifth lesson, now all you need to do is practice these concepts and if you have any queries then feel free to comment on this post.


next Previous Lesson --- Next Lesson next

08 January 2011

Php For Beginners (Lesson 4d)

If you are reading this lesson then I assume that you have read all of my previous lessons, so coming directly to the point, I will be discussing logical operators and their usage in php.

And Operator (&&)
"&&" operator is used to take the union of two or more conditions and returns true if and only if all the conditions are true.
<?php
$a=5;
$b=6;
if ($a==5 && $b==6)
    echo "true"; //Outputs true
?>
Or Operator (||)
"||" operator returns true if any of the condition is true.
<?php
$a=5;
$b=6;
if ($a==5 || $b==5)
   echo "true"; //Outputs true 
?>
Not Operator (!)
"!" operator returns true when no condition is true.
<?php
$a=5;
$b=6;
if !($a==$b)
   echo "true"; //Outputs true
?> 
Well that is all we need to know about Operators in php, keep reading and feel free to ask questions through comments.


next Previous Lesson --- Next Lesson next

04 January 2011

Php For Beginners (Lesson 4)

Overview

In this lesson we will be discussing operators in php. Operators are used to perform certain operations on some variables/operands. Example of operators includes mathematical operators such as “+”, “-”,”=” and etc.

Operators are divided into four categories:

• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• Logical Operators

In this lesson I will be discussing Arithmetic operators only, the rest of the operators will be discussed in the upcoming lectures.

Arithmatic Operators

Arithmetic operators are used to perform arithmetic operations such as addition, subtraction and etc.

Addition Operator (+)
< ?php
$a=5;
$b=6;
echo $a+$b; // outputs 11
?>

Subtraction Operator (-)
< ?php
$a=10;
$b=6;
echo $a-$b; // outputs 4
?>

Multiplication Operator (*)
< ?php
$a=5;
$b=6;
echo $a*$b; // outputs 30
?>

Division Operator (/)
< ?php
$a=30;
$b=6;
echo $a/$b; // outputs 5
?>

Modulus Operator (%)

Modulus operator returns the remainder of division.
< ?php
$a=5;
$b=2;
echo $a%$b; // outputs 1
?>

Increment Operator (++)
< ?php
$a=5;
$a++;
echo $a; // outputs 6
?>

Decrement Operator (--)
< ?php
$a=5;
$a—;
echo $a; // outputs 4
?>

Well thats all for the fourth lesson, I hope u guys are enjoying my lessons and learning some php and if you have any query then feel free to comment and ask, I will try my level best to answer your question.


next Previous Lesson --- Next Lesson next

02 January 2011

Php For Beginners (Lesson 2)

Overview

Since you are reading this article therefore I am assuming that you have read my first lesson (If not, I would recommend that you should read lesson 1 first). So I will be continuing from where I left in lesson 1 and in order to give you an idea what you are going to learn in this lesson, I am stating just the names of those

Topics:

• Comments
• Constants
• Variables

Comments

In php there are three ways to write comments. Two of them are used to comment only one line and the third one can be used to comment multiple lines. First method is to simply write “#” in the start of the line that you want to comment, in second method you need to write “//” in the start of the line you need to comment and in the third method you need to encapsulate multiple lines that are to be commented in between “/*” and “*/”. “/*” is used to start the comment and “*/” is used to end the comment.

Example
< ?php
echo “test line”; #This is a one line comment
echo “Another test line”; //This is also a one line comment
/*This is a multi line comment
Another line….. 
Another line…..
End of multi lines comment*/
?>

Constants

As the name suggests, Constants have a fixed value that once set cannot be changed or re-defined. In php if you need to set a constant then you have to use define() function, but remember once a constant is defined, it can never be changed or undefined.

Syntax

define(constant name,constant value,case_insensitive)

Example # 1
< ?php
define(“test_constant”,”Hey There!”);
echo test_constant; // outputs “Hey There!”
echo TEST_CONSTANT; // outputs “TEST_CONSTANT”
?>

In above mentioned example we used only two arguments (name & value) and constants are by default case sensitive so first line will give the write output but the second line will simply display the name of the constant.

Example # 2
< ?php
define(“test_constant”,”Hey There!”,TRUE);
echo test_constant; // outputs “Hey There!”
echo TEST_CONSTANT; // outputs “Hey There!”
?>

In this example as we have set the third argument (case insensitive) to TRUE so this constant is now case insensitive and both lines will output the right content.

Variables

Variables, as their name suggests, are used to store values that can be changed. In php variables names are case sensitive and are represented by a dollar sign followed by the name of the variable. In contrast to constants, variables don’t need define() function to store values, instead variables can be easily assigned values by using “=”.

Example
< ?php
$variable_name=”Test content”;
echo $variable_name; // outputs “Test Content”
$another_variable=85;
echo $another_variable; // outputs 85
?>

Well guys, I guess this is all for the second lesson now you guys should practice these concepts because “Practice Makes A Man Perfect”.

By the way I am deliberately going slow and covering fewer concepts in these lessons so that it will be easier for the beginners to digest them. ;)

References

http://www.php.net
http://www.w3schools.com

next Previous Lesson --- Next Lesson next

29 December 2010

Php For Beginners (Lesson 1)

Definition
PHP: Hypertext Preprocessor is a widely used, general-purpose server side scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document.

Installation
In order to run php on your own machine you need to install following three things:
  • Apache Server
  • Php
  • MySQL
But to make this lesson easier I will not discuss explicit installation of all the three things, rather I would tell you guys a simpler way of doing this. For this purpose we can simply use WAMP/XAMPP Server which is an all in one package of apache, php & MySQL.

WAMP Server can be downloaded from the link below:
http://sourceforge.net/projects/wampserver/files/WampServer%202/WampServer%202.0/WampServer2.0i.exe/download

Or you can download XAMPP Server for windows from the below mentioned link:
http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/xampp-win32-1.7.3.exe/download

After downloading simply follow the steps to install WAMP/XAMPP server on your PC. Once the installation is complete you can test it by typing http://localhost/ in the explorer. This should open a default page for the WAMP/XAMPP Server. If the default page does not come up then try restarting the WAMP/XAMPP Server.

How to Start
Now that php is successfully installed on your PC, we need to know where you should type the php code. Well you can use Notepad, Dreamweaver or any other editor to write in the php code. I would recommend that you use Dreamweaver because it’s a lot easier to use.

After writing the code, you need to safe the file with a .php extension. For example you can save your file as test.php in the respective server folder.

In case of WAMP Server you need to save your php files in wamp > www directory. The exact path of this directory is:
C:\wamp\www\test.php

Whereas if you are using XAMPP Server then you need to safe your php files in the XAMPP > htdocs directory. The exact path should be:
C:\xampp\htdocs\test.php

Now you can run your php file by typing http://localhost/test.php in your browser.

Syntax
A Php code will always be encapsulated between "<?php " and "?>" each instruction will be terminated by semicolon (;).

Example
<?php
some php code....;
some php code....;
some php code....;
?>
Hello World Example
In php if you want to display something on the browser’s screen, you need to use “echo” function.

Syntax
echo “anything you like to display on the screen”;

Example
<?php 
echo "Hello World!";
?> 
Ok guys that is all for the first lesson now you should practice and play with this code to get used to with the php syntax.

References
http://www.wikipedia.com
http://www.w3schools.com 


Next Lesson next