php Array made simple

An array is a special kind of variable that contains multiple values.The simplest way to create an array in PHP is to use the array command:

$myArray = array('one', 2, '3');

This code creates an array called $myArray that contains three values: ‘one’, 2, and ‘3’. Just like an ordinary variable, each space in an array can contain any type of value. In this case, the first and third spaces contain strings, while the second contains a number. To access a value stored in an array, you need to know its index. Typically, arrays use numbers as indices to point to the values they contain, starting with zero. That
is, the first value (or element) of an array has index 0, the second has index 1, the third has index 2, and so on. Therefore, the index of the nth element of an array is n–1. Once you know the index of the value you’re interested in, you can retrieve that value by placing that index in square brackets after the array variable name:

 echo $myArray[0];       // outputs 'one'
 echo $myArray[1];       // outputs '2'
 echo $myArray[2];       // outputs '3'

Each value stored in an array is called an element of that array. You can use an index in square brackets to add new elements, or assign new values to existing array elements:

 $myArray[] = 'the fifth element';
 echo $myArray[4];       // outputs 'the fifth element'

well these type of arrays are called numeric arrays, as its indexes are all numeric.There is yet another type of array called associative arrays which uses characters or strings as index infact this type of arrays are more meaningful for example look at the code below :

 $birthdays = array('Kevin' => '1978-04-12', 'Stephanie' =>
 '1980-05-16', 'David' => '1983-09-09');

here we created an array $birthdays with three indexes ‘kevin’,’stephanie’ & ‘David’ and we use ‘=>’ to store the corresponding birthdays.if we want to retrieve kevins’s birthday we simply have use its corresponding key or index like this :

 echo 'Kevin birthday is: ' . $birthdays['Kevin'];

now just change the key to get the corresponding birthday and you will get the corresponding birthdays simple.

if you want to know more about php array head to php.net its the official site of php.

See you next time with more helpful tutorials on php and web if you have any questions post a comment…

php variables piece of cake !

variables are like container you can store any data like numbers,strings and letter or even array in it and retrieve it latter an example would give you a good idea about it.Now open Notepad++ and write the following lines of code and save in htdocs/helloworld folder you have made in the earlier tutorial.

 <?php

$variable1 =  12;

$variable2 = 'name';

$variable3 = 'What is your name ?';

echo $variable1;

echo '</br>';

echo $variable2;

echo '</br>';

echo $variable3;

?>

the browser output should be as shown below

variable

now thats that  (if you don’t know what does “echo” does its simple displays whatever written in the ‘ ‘or ” ”  before it to the browser.)

If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!

Note: Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_number are different variables in PHP’s eyes.

PHP Variable Naming Conventions

There are a few rules that you need to follow when choosing a name for your PHP variables.

  • PHP variables must start with a letter or underscore “_”.
  • PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • Variables with more than one word should be separated with underscores. $my_variable
  • Variables with more than one word can also be distinguished with capitalization. $myVariable

if you want to know more about php head to php.net its the official site of php.

See you next time with more helpfull tutorials on php and web if you have any questions post a comment…

php hello world program

First of all php stands for Hypertext Preprocessor.PHP is one of the most popular server side scripting language used by web developers all over the world to build websites and web application.Most of big guns of web world use PHP the most famous one i can think from the top of my head is FACEBOOK.
Now lets get back to our php script first of all you will need a text editor although notepad will do the job but i recommand Notepad++ ,its simple with nice syntax highlighting and great for beginners.Now quickly type this in notepad++.

<?php

echo 'Hello World';

?>

save this file with a .php extension(myprogram.php).With this three lines of code you have successfully written your first php script.

Since writing is over now its time to run the script on your pc.It wont run by double clicking on it you will need to run it on a server for this download XAMPP an install it.If you don’t know how to install XAMPP follow this link.

By default XAMPP is installed under C:\xampp  now  copy your php script you wrote earlier now browse to “C:\xampp\htdocs\” create a new folder here with any name say “helloworld”.

folder

and past it on “C:\xampp\htdocs\helloworld\”.

folderpast

now thats that !! open up ur browser any browser and type in the url http://localhost/{your folder name in htdocs}/{your php file name}” for eg. http://localhost/helloworld/myprogram.php“.

browser

wolla !!  you have successfully written and run your first php script

phphelloworld

if you want to know more about php head to php.net its the official site of php.

See you next time with more helpfull tutorials on php and web if you have any questions post a comment and sorry if i made any mistakes its my first time writing a tutorial.