Search This Blog

Tuesday, December 22, 2009

PHP Arrays

What is Array?
  • An array is a variable that holds multiple values of the same type. 
  • An array in PHP is an ordered map that associates values to keys.
Syntax
     An array can be created by the array() language construct. It takes as parameters any number of comma-separated key  => value pairs.
     
 i.e. 
$array = array(key1 => value1 , key2 => value2 , ... , keyn => valuen);

(Note: key may be integer or string. If you do not define the key, then PHP parser automatically takes  key as Integer and it will starts from 0.)


Types of Array:
In PHP, there are three kind of arrays:

1. Associative or Indexed array: An array where each ID key is associated with a value. Its kind of array whose keys can be numeric or string.             
                                                                                  

PHP Code: (Example of Associative array)

<?php
//Associative Array

$array = array("Fruit" =>"Orange",                         "Flower" => "Rose",                                              "Vegetable" => "Potato" );


print_r($array);

?>

Output:
Array
(
    [Fruit] => Orange
    [Flower] => Rose
    [Vegetable] => Potato
)


2. Numeric array: An array with a numeric index. Its kind of array whose keys can only be numeric.                                  
PHP Code: (Example of Numeric array)

<?php
//Numeric Array

$array = array("Orange","Rose","Potato" );


print_r($array);

?>

Output:
Array
(
    [0] => Orange
    [1] => Rose
    [2] => Potato
)

3. Multidimensional array: An array containing one or more arrays.

PHP Code: (Example of Multidimensional array)

<?php
//Multidimensional Array

$array= array
  (
  "Flowers"=>array
   (
  "Rose","Lotus"                                                     ),
  "Fruits"=>array
   (
  "Orange","Pineapple","Banana","Apple"
   ),
  "Vegetables"=>array
   (
  "Potato",
  "Tomato"                                                              )
 ); 


print_r($array);

?>

Output:
Array
(
    [Flowers] => Array
        (
            [0] => Rose
            [1] => Lotus
        )

    [Fruits] => Array
        (
            [0] => Orange
            [1] => Pineapple
            [2] => Banana
            [3] => Apple
        )

    [Vegetables] => Array
        (
            [0] => Potato
            [1] => Tomato
        )

)


What we use in PHP to Display Array:
  • print_r displays information about a variable in a way that's readable by humans.We use print_r to display array in PHP.
Regards,
Rohit Modi



Monday, December 21, 2009

Strings and Comments

What is String?
String comprizes of One or More Characters. A string can be used directly in a function or it can be stored in a variable. We use "echo" or "print" to display the string.


PHP Code:
<?php
echo "Hi This is First String!";
?>


Output:
Hi This is First String!


Concatenating Two Strings: 
The concatenation operator (.)  is used to put two string values together.

Suppose we have two strings lets say string one contain Name and string second contain Surname and if you want to concate these two strings then we need to use the concatenation operator (.) 

PHP Code:
<?php
$name = "Cristiano";
$surname = "Ronaldo";
echo $name." ".$surname;
?>


Output:
Cristiano Ronaldo

Careful when using Quotes
You must be very careful when using quotes in strings! Echo uses quotes to define the beginning and end of the string, so you must use one of the following if your string contains quotations:
  • Don't use quotes inside your string.
  • Escape your quotes that are within the string with a backslash. To escape a quote just place a backslash directly before the quotation mark, i.e. \"
  • Use single quotes (apostrophes ') for quotes inside your string.
PHP Code:

<?php

// Wrong Method - Gives an Error
echo "Hi I am writing String";  

// Correct Method to write String
//Here we used Quotes
echo "Hi I am writing String";  
 
//or
//Here we used an apostrophe '
echo "Hi I am writing String";  
?>



Comments in PHP:
Comments are very useful in PHP to trace the code in future.
There are two ways to wrie comments in PHP
  • /* Comment                                                                                                                                             Comment                                                                                                                                             Comment                                                                                                                                             Comment */
  • // Comment
(Note: Inline Comment - If you are using this method then you need to write // in each and every line. If you want to comment one or two lines then use // and if you want to comment some particular portion of the php code then use '/* */' )

PHP Code:
<?php
$name = "Cristiano";      // Hi This is Comment
$surname = "Ronaldo";
echo $name." ".$surname;

/* I have used Concatenation Operator to Join Two Strings */

?>


Regards,
Rohit Modi


Sunday, December 20, 2009

PHP Variables


Hi Guys,

Today we will learn about PHP Variables.

PHP Variables:


Before learning any programming language, you should have knowledge of Variables. So first let's starts with the definition of Variable.

What is Variable?
"Variable is like a container which stores value, such as string value "Hi How are you!" or integer value 1.Instead of typing out the actual value over and over again in your code, assign that value to a variable and after that you can use that variable throughout the code."

How to define variable?
"We use dollar($) sign to define variable in PHP. So in PHP, every variable must starts with dollar($) sign. So if you forgot to put dollar sign in front of variable name, then php parser wont recognize variable."
          $variable_name  = Value ;


         For Example:
         $a = "Hi How are you!";
         $b = 4;
      
(Important Note: PHP does not require variables to be declared before being initialized. So you need not to declare variable as string or integer as we defined in C, C++. PHP Parser automatic parse variable as a string or as a integer as an when you initialized the variable.)

PHP Code:
<?php

$variable_name = "Hi How are you!";

?>


Output:
Hi How are you!

Variable Naming Convention:
Every programming language have some standard rules that must be follow when choosing a name for Variable.
Here are some standard rules that need to be follow for choosing name for PHP Variable.
  • PHP variables must start with a letter or underscore "_".
  • Variables with more than one word should be separated with underscores. e.g $variable_name_surname
  • PHP variables are case-sensitive. e.g $variable and $Variable both are different
  • PHP variables may only be comprised of alpha-numeric characters and underscores.                    e.g: a-z, A-Z, 0-9, or _ .


Thanks & Regards,
Rohit


Saturday, December 19, 2009

PHP Syntax and Sample PHP Program

Hi Guys,
Hope you understood how to install WAMP.
Now today we start with the basic program of how to print data in PHP.

Before writing PHP program, note down few basic points which will helpful to you.
PHP Syntax
  • PHP's syntax and semantics are similar to most other programming languages C, C++.
  • All PHP code must be contained starting '<?php' and closing tag '?>'. PHP only parse that code which is written inside starting '<?php' and closing tag '?>'. 



        
    Standard Form 

       <?php
        PHP Code
       ?>


        or   Shorthand Form        

        <?
        PHP Code
       ?>



    Shorthand Form requires Shorthand Support to be enabled on your server. So I suggest you that you use the standard form (which includes the ?php) rather than the shorthand form. This will ensure that your scripts will work, even when running on other servers with different settings.


  • Every PHP statements ends with the semicolon. So semicolon should never be forgotten.
How to Save PHP Page
If you write PHP code inside HTML and want the web browser to interpret it correctly, then you must save the file with a .php extension, instead of the standard .html extension.

If there is some PHP code in HTML file then Instead of index.html, it should be index.php.


Simple PHP Program
Code:



<html>
<head>
<title>My First PHP Program</title>
</head>
<body>
<?php
echo "Hello Guys, I an writing my First PHP Program";
?>
</body>
</html>

Output of Above Code:

 Hello Guys, I an writing my First PHP Program!

Guys if you have any doubts regarding PHP, feel free to contact me. My email id is rohitpmodi@gmail.com or write comments. I will try my best to solve your doubts.

Regards,
Rohit Modi

Friday, December 18, 2009

Overview of PHP

Definition of PHP?
  • Hyper Text Preprocessor.
  • Programming Language like ASP.NET.
  • Server Side Scripting Language.
  • Used for Developing Dynamic Web Application.
  • Can be embedded within HTML.
PHP File
  • PHP file have file extension of ".php", ".php3".
  • PHP file contains Text, PHP Contents, HTML Contents.
  • Server needed to run PHP File.
Advantages of PHP Over Other Languages
  • PHP is Open Source. So like ASP.NET, you need not to pay money to buy PHP.
  • Compatible with almost all the servers.
  • Its most of the syntax  are like C, C++. So its very easy to learn and understand this language.
  • PHP has weakly typed data, which tends to result in fewer data collisions than strongly typed languages.

What you need to run PHP ?
  • Server : WAMP  You need to have WAMP server to execute PHP File.   WAMP Server. (Follow my previous post to know how  to Install WAMP in your Machine)

  • Editor:   Macro media Dream weaver (Recommended), Edit Plus, Macro media Home site+  

Thursday, December 17, 2009

WAMP Server Set Up Guide

Follows the following steps to setting up a WAMP server in your Machine.

Step 1: 

First go to http://www.wampserver.com/en/download.php
Once you go on that site look for the below box to appear and Click on Download WAMP Server 2.0








Step 2:
Download the file to your Machine. It is approx 20 MB file, it will take a few minutes to download.


Step 3:
After downloading WAMP, you will see this link, double click on that link to start the Installation



 

 
 
 
 
 
 
 
 
 

Step 4:
After executing all the above steps, WAMP is ready to use.After installing WAMP, The below Blog appears on the bottom left the screen.

Step 5:
Once the server is running, you will see the above icon and the taskbar for the WAMP Server.
 

Now in my next post I will explain you how to create PHP Page


If you still have doubts in installing WAMP server then refer this Video Tutorial:
http://www.youtube.com/watch?v=h1w-30JAIHs



Regards,
Rohit Modi

Wednesday, December 16, 2009

PHP Introduction

Hi all,

Now today is I will give you the brief overview of PHP in my own worlds.
PHP Origionally stood for "Personal Home Page" later becomes "Hyper Text Preprocessor". It was created by Rasmus Lerdorf in 1995. PHP Follows "De Facto" standards means it is universaly accepted by People. PHP is open source Language. So its Free. There wont be any licence needed to run PHP Application.

PHP is widely used,general purpose server side scripting language, used for creating Dynamic Web Applications. It can be embedded into HTML and generally runs on Web Server. In terms of keywords and language syntax, PHP is similar to most high level languages that follow the C style syntax. If conditions, for and while loops, and function returns are similar in syntax to languages such as C, C++, Java and Perl. So if you are familiar with C, C++ languages then its very easy for you to learn PHP within a week. So don't wait and starts learning PHP from today. In the next post, I will give you more idea about PHP.

Regards,
Rohit Modi

Tuesday, December 15, 2009

Purpose for Creating this Blog

Hi all,
Before posting anything, I would like like to introduce my self. I am Rohit Modi. I have Completed my Graduation from Nirma University, Ahmedabad. I have experience of 2 years in IT field, especially in PHP.

The purpose of this blog is to give you brief idea about PHP and whats new in PHP. So anybody who wants to learn PHP without help of books or any instructor then follow my blog. I will post everything A to Z about PHP in my Blog.

Regards,
Rohit Modi
Website Visitors: blogger visitor