Tag Archives: php tutorial

php-equals-operator

PHP Equal operator

PHP Equal operator

There are three equal operator or sign available in PHP.

php equals operator

  1. Assignment or single equal (=)
  2. Equality or double equal(==)
  3. Identical or triple equal(===)

Let us describe one by one with the help of the example.

PHP Single equal operator

Single equal operator is basically used for assigning the value to on the right to the variable on the left, as shown in the below example.

Syntax of PHP single equal operator

<?php

echo "Single Equal operator<br>";
$firstNumber=5;
$SecondNumber=10;
$sum=$firstNumber+$SecondNumber;
echo " Sum=$sum";

?>
Single Equal operator
Sum=15

Double equal operator

Double equal operator is used to checks the left and right value are equal, as shown in the example below.

Example of Double equal operator

<?php

echo "Double Equal operator<br>";
$a=5;
$b=10;
if($a==$b)
{
echo "Both are equal";	
}
else
{
echo "Both are not equal";	
}

?>

Output

Double Equal operator
Both are not equal

Triple equal operator

Triple equal operator is used to checks the left and right value are equal with same data type that means left value and right value must be in same data type.

Example of Triple equal operator

<?php

echo "Triple- Equal operator<br>";
$a=5;
$b=5.0;
if($a===$b)
{
echo "Both are equal";	
}
else
{
echo "Both are not equal";	
}

?>

Output

Triple- Equal operator
Both are not equal


Redirect URL in PHP

Redirect URL in PHP

Header function

This is very simple way to redirect a web page to another web page using PHP at server side. To redirect your site visitors to a new page or new website, you just need to add a header function is first line of the web page. It works almost all browser.

 

redirect-in-php

Syntax

<?php

Header("Location: Web page address");

?>

Example of redirect

<?php
/* Redirect browser from current web page to another */

Header("Location: another-page.php");
?>

It is extremely important that you understand that the header() function must be called before any actual output is displayed to the browser.

Header function must be first of you web page because no actual output is sent, as shown below

Example

<?php
echo "Hello world";
echo "ptutorial.com";
header("Location: new-page.php");
exit;
?>

Note: Make sure all the actual code after the header function not get executed so don’t write anything after header function.

Absolute / relative path

If the destination web page is on the other server include complete URL like that http://www.ptutorial.com.

Example

<?php
header("Location: http://www.ptutorial.com");
exit;
?>

You can also use relative path for redirection like below shown.

Example

<?php
header("Location: ../new.php");
exit;
?>

Redirect after particular time

Redirect a webpage to another web page after defined time, use below syntax.

Example

<?php
  header( "refresh:5;url=http://www.ptutorial.com" );
  exit;
?>

Redirect with proper message

Redirect a webpage to another web page after defined time and proper message like “you are redirected after 5 second or any other”, use below syntax

Example

<?php
   header( "refresh:4; url=http://www.ptutorial.com" );
   echo "You will be redirected after 4 second";
?>

Use of redirect?

    • Redirect after form submission
    • Drop down menus
    • Pull down menus

If you want to learn more about functions and function library please visit
php tutorial



PHP Here document

Here document |pTutorial

Here document use as a special input output redirection to feed a command list to an interactive program.

It is the way to large block of code into a single variable.

Syntax of here document

<?php
$variable_name=<<<EOF
Code here
-------------------
echo "php tutorial";
echo"www.ptutorial.com";
-------------------
-------------------
-------------------
-------------------
-------------------

EOF;
Echo $variable_name;
?>

Following two example explain the concept of here document

Simple example with a lot of echo statement

<?php

Echo” WWW.ptutorial.com”;
Echo “
<ul>”;
	Echo”<LI>PHP tutorial</li>
”;
	Echo”<LI>java tutorial</li>
”;
              Echo”<LI>C example</li>
”;
echo“</uL>”;
echo “
<h3>Created by ptutorial.com</h3>
”;

?>

Output

WWW.ptutorial.com

  • PHP tutorial
  • java tutorial
  • C example

Created by ptutorial.com

Example using here technique

<?php
$here_doc = <<<EOF
<h3> WWW.ptutorial.com</h3>
<ul>
	<LI>PHP tutorial</li>
<LI>java tutorial</li>
<LI>C example</li>
</uL>
<h3>Created by ptutorial.com</h3>
EOF;

echo ($here_doc);
?>

Output

WWW.ptutorial.com

PHP tutorial
java tutorial
C example
Created by ptutorial.com

EOF denote the start and end point of string. This can be any combination of alphanumeric character. All the rule of making variable are applied here.

If you want to learn more about PHP visit : Php tutorial