Tuesday 3 January 2017

Learn PHP 1

PHP Example

It is very easy to create a simple PHP example. To do so, create a file and write HTML tags + PHP code and save this file with .php extension.
All PHP code goes between php tag. A syntax of PHP tag is given below:
1.    <?php   
2.    //your code here  
3.    ?>  
Let's see a simple PHP example where we are writing some text using PHP echo command.

File: first.php
1.    <!DOCTYPE>  
2.    <html>  
3.    <body>  
4.    <?php  
5.    echo "<h2>Hello First PHP</h2>";  
6.    ?>  
7.    </body>  
8.    </html>  
Output:

Hello First PHP

PHP Echo

PHP echo is a language construct not a function, so you don't need to use parenthesis with it. But if you want to use more than one parameters, it is required to use parenthesis.
The syntax of PHP echo is given below:
1.    void echo ( string $arg1 [, string $... ] )  
PHP echo statement can be used to print string, multi line strings, escaping characters, variable, array etc.

PHP echo: printing string

File: echo1.php

1.    <?php  
2.    echo "Hello by PHP echo";  
3.    ?>  

Output:

Hello by PHP echo

PHP echo: printing multi line string

File: echo2.php

1.    <?php  
2.    echo "Hello by PHP echo  
3.    this is multi line  
4.    text printed by   
5.    PHP echo statement  
6.    ";  
7.    ?>  
Output:
Hello by PHP echo this is multi line text printed by PHP echo statement

PHP echo: printing escaping characters

File: echo3.php

1.    <?php  
2.    echo "Hello escape \"sequence\" characters";  
3.    ?>  

Output:
Hello escape "sequence" characters

PHP echo: printing variable value

File: echo4.php

1.    <?php  
2.    $msg="Hello Jsk Suresh PHP";  
3.    echo "Message is: $msg";    
4.    ?>  
Output:
Message is: Hello Jsk Suresh PHP

PHP Print

Like PHP echo, PHP print is a language construct, so you don't need to use parenthesis with the argument list. Unlike echo, it always returns 1.
The syntax of PHP print is given below:
1.    int print(string $arg)  
PHP print statement can be used to print string, multi line strings, escaping characters, variable, array etc.

PHP print: printing string

File: print1.php

1.    <?php  
2.    print "Hello by PHP print ";  
3.    print ("Hello by PHP print()");  
4.    ?>  

Output:
Hello by PHP print Hello by PHP print()

PHP print: printing multi line string

File: print2.php

1.    <?php  
2.    print "Hello by PHP print  
3.    this is multi line  
4.    text printed by   
5.    PHP print statement  
6.    ";  
7.    ?>  
Output:
Hello by PHP print this is multi line text printed by PHP print statement

PHP print: printing escaping characters

File: print3.php

1.    <?php  
2.    print "Hello escape \"sequence\" characters by PHP print";  
3.    ?>  

Output:
Hello escape "sequence" characters by PHP print

PHP print: printing variable value

File: print4.php

1.    <?php  
2.    $msg="Hello print() in PHP";  
3.    print "Message is: $msg";    
4.    ?>  
Output:

Message is: Hello print() in PHP

PHP Variables

A variable in PHP is a name of memory location that holds data. A variable is a temporary storage that is used to store data temporarily.
In PHP, a variable is declared using $ sign followed by variable name.
Syntax of declaring a variable in PHP is given below:
1.    $variablename=value;  

PHP Variable: Declaring string, integer and float

Let's see the example to store string, integer and float values in PHP variables.

File: variable1.php

1.    <?php  
2.    $str="hello string";  
3.    $x=200;  
4.    $y=44.6;  
5.    echo "string is: $str <br/>";  
6.    echo "integer is: $x <br/>";  
7.    echo "float is: $y <br/>";  
8.    ?>  
Output:
string is: hello string
integer is: 200
float is: 44.6 

PHP Variable: Sum of two variables

File: variable2.php

1.    <?php  
2.    $x=5;  
3.    $y=6;  
4.    $z=$x+$y;  
5.    echo $z;  
6.    ?>  

Output:
11

PHP Variable: case sensitive

In PHP, variable names are case sensitive. So variable name "color" is different from Color, COLOR, COLor etc.

File: variable3.php

1.    <?php  
2.    $color="red";  
3.    echo "My car is " . $color . "<br>";  
4.    echo "My house is " . $COLOR . "<br>";  
5.    echo "My boat is " . $coLOR . "<br>";  
6.    ?>  

Output:

My car is red
Notice: Undefined variable: COLOR in C:\wamp\www\variable.php on line 4
My house is 
Notice: Undefined variable: coLOR in C:\wamp\www\variable.php on line 5
My boat is 

PHP Variable: Rules

PHP variables must start with letter or underscore only.
PHP variable can't be start with numbers and special symbols.

File: variablevalid.php

1.    <?php  
2.    $a="hello";//letter (valid)  
3.    $_b="hello";//underscore (valid)  
4.      
5.    echo "$a <br/> $_b";  
6.    ?>  
Output:
hello 
hello

File: variableinvalid.php

1.    <?php  
2.    $4c="hello";//number (invalid)  
3.    $*d="hello";//special symbol (invalid)  
4.      
5.    echo "$4c <br/> $*d";  
6.    ?>  

Output:
Parse error: syntax error, unexpected '4' (T_LNUMBER), expecting variable (T_VARIABLE)

 or '$' in C:\wamp\www\variableinvalid.php on line 2

1 comment:

  1. Get all Technology and Programming Learn latest technology, software development skills, b.tech, MCA and BCA

    computer programming training.

    ReplyDelete