Search This Blog

Friday, January 8, 2016

Difference Between $var and $$var in PHP

Difference Between $var and $$var in PHP

$$var uses the value of the variable whose name is the value of $var.
It means $$var is known as reference variable where as $var is normal variable.
It allows you to have a “variable’s variable” – the program can create the variable name the same way it can create any other string. .
Eg i
Output
Rajeev
Sanjeev
Sanjeev
In the above example
$name is just a variable with string value=”Rajeev”.
$$name is reference variable . $$name uses the value of the variable whose name is the value of $name.
echo $name print the value: rajeev
echo $$name print the value:Sanjeev \ value of this($name) variable is act as reference of second variable($$name).
echo $rajeev print the value :Sanjeev \ Here $rajeev is also act as reference variable.

Eg ii
Output
100
200
200
In the above example
You first assign the value of a variable, ($x) as the name of another variable.
When you set $x to a value, it will replace that variable name with the value of the variable you provide.
variable $x hold value = 100.
$$x(reference variable) hold value = 200. now we want to print the value.
echo $x gives output:100
echo $$x gives output:200.
echo $100 gives value.200. because it also act as a reference variable for value = 200.

Eg iii
Output
Rajeev
Sanjeev
Sanjeev

Eg iv
Output
Ravi
Ranjan
Rexx
In the above example
variable $name hold value =”ravi”
variable ${ $name } hold value =”Ranjan” // it also declare like ${Ravi}.
variable ${$ {$name} } hold value =”Rexx” // it act as “variable’s of variable of variable” reference.
echo $name show output:ravi
echo ${ $name } show output:Ranjan.
echo ${ $ {$name} } show output :Rexx

Variables in PHP

Variables in PHP

Variable is nothing it is just name of the memory location.
A Variable is simply a container i.e used to store both numeric and non-numeric information.

Rules for Variable declaration

  • Variables in PHP starts with a dollar($) sign, followed by the name of the variable.
  • The variable name must begin with a letter or the underscore character.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • A variable name should not contain space

Assigning Values to Variables

Assigning a value to a variable in PHP is quite east: use the equality(=) symbol, which also to the PHP’s assignment operators.
This assign value on the right side of the equation to the variable on the left.
A variable is created the moment you assign a value to it:
Eg i
Output
Honda
In the above example
Create a variable ($mycar)containing a string with value=”Honda”.
To print the carname pass $mycar inside echo statement.

PHP Concatenation

Eg ii (concatenate variable with string)
Output
Honda City is riding
In the above example
Variable($mycar) hold value=”honda city”. Now we wants to concatenate variable with string.
pass this variable($mycar) inside echo statement.
To concatenate this with a string(“is riding”) use dot(.) between variable name and string.
The output will be displayed : Honda City is riding
Eg iii (Sum of two numbers)
Output
Sum = 300
In the above example
Declare $first , $second variable with value=100, 200 respectively.
Now add these two numbers using arithmetic operator (“+”).
sum of these two variable result stored in a third variable($sum).
Now print the sum passing ($third) with echo statement with a string.
Eg iv (Subtraction of two numbers)
Output
Subtraction = 500
In the above example
We perform subtraction using variables( $first, $second) with vale=1000,500.
Subtract second variable from first, result is hold by third variable($third) .
Print this third variable passing with echo statement.

Destroying PHP Variables

To destroy a variable, pass the variable to PHP’s unset( ) function.
as in the following example:
Eg v
Output
steve
In the above example
declare variable $name hold value=”steve”. In this program we used unset() function to delete a particular variable.
first it show the output: “steve”, because we pass unset function after echo statement.
Now pass variable name inside unset($name) function output will show an Notice error(Variable is undefined).
Eg vi
Output
Sum = 300
Sum = Notice error undefined third variable
Note : Trying to access or use a variable that’s been unset( ), as in the preceding script, will result in a PHP “undefined variable” error message.
This message may or may not be visible in the output page, depending on how your PHP error reporting level is configured.

Variable names in PHP are case-sensitive

Output
rexx
rahul
Variable names in PHP are case-sensitive. As a result, $name refers to a different variable than does $NAME.

Inspecting Variable Contents(Variable Property)

PHP offers the var_dump( ) function, which accepts a variable and X-rays it for you.
Here’s an example
Eg vii ( For String value )
Output
string ‘Fiona’ (length=5) int 25
In the above example
We use var_dump( ) function to check the Contents(property) of variable, $name hold a string value =”Fiona” while $age hold an integer value = 25.
Now pass this variable inside var_dump($name,$age) function .
It show all information related this(data type of variable, length(only string), value)
Eg viii ( For Integer values )
Output
int 300
Eg ix ( For Floating value )
Output
float 300.7
In the above example
variables first hold $first=100.5, second hold $second=200.2.
Now add these two values, result is stored in third variable($third).
Pass this variable inside var_dump($third) to check the content.
output will float 300.7
Eg x ( For Boolean value )
Output
Boolean true
In the above variable
$bool with value=”true”. var_dump($bool) function is used to display the result
so output will display Boolean true, because variable holds a Boolean value

Comments in PHP

Comments in PHP

A comment is non-executable lines. comment is used to write description  for your own understanding.Browser doesn’t read the comments.
There are two types of comments used in php

1. Single line comments :

Single line comment used for short explanations.
Declaration of Single line comment are two types
Either Begin with(#) Or backslash(//)
In the above Example.
*First and second line comments begin with hash(#).
*The third one is begin with(//).
If we check the output of the given example.
Browser show blank page. Because comments are always non-executable..

another Eg of Single line Comment
Output
welcome
In the above Example.
We declare a variable to store the String(“welcome”)
In second line we concatenate string(“student”) with the Previous string(“welcome”)
In third line we check the output.It shows Welcome Only. Because the second line statement has already specify a comment statement.
So it can’t take the string(“student”) as a declaration.

2. Multi-lines comments :

Multi lines comments  used to comment multiple lines.
Here we can give comments in bulk
The bulk comments are enclose within (/*…..*/)
The all lines which is define in php evironment are Multiline comments.
it is non-executable.Because it enlose with Multiline comments statement.

another eg of Multi-line comments
Output
Hello user how are you ?

PHP Environment

PHP Environment Start – End

syntax is a way to representation of PHP script.
Basically it gives the primary idea to specify the code format.
It also specify the area of a written code.
There are three ways to start PHP environment.

The most commonly and effective  PHP  syntax:

In the given Example
It begin with (< ?php) and End with(? >).
echo statement is used to print the given string. Mandatory closing in(“ABCD”)double quotes.
Terminate the script with semicolon because semicolon is known as terminator.

Short or short-open tags look like this:

In the given above example..
We use Short tags. start and end with () respectively.
Declare the statement in between (), to display the output on browser
short open tags smoothly works on XAMPP server but Face problem on wamp server
if you are using  WAMP Server First you have to Set the short_open_tag setting in your php.ini file to on

HTML script tags:

PHP is the server side Scripting language. So HTML script is also used to start PHP environment like other scripting language.