Category Archives: PHP

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



Exception handling

Exception handling in PHP

Exception handling

An exception is an event that occurs at time of execution of program / script that interrupt the execution of the program. PHP 5 introduce the mechanism of exception that handle the error condition in case of object.

 php exception handling

PHP 5 has the exception model similar to the other object oriented programming languages and provide three keywords to handle the exception

  • Try
  • Catch
  • Throw

Try block

PHP uses a keyword try to preface a block of code that is likely to cause an error condition.

Syntax


try
{
Code here;
}

If any exception occur in try block an exception handler associated with it handlers that exception. To associate an exception with try block you must put catch block after try blog.

Catch Block

It is used to catch the exception you can write multiple catch block for single try block. Each catch block contains handler of try block. Catch block code is executed when exception occur in try block.

Syntax

try
{
Code here;
}
Catch(ExceptionType name)
{
Code here;
}

Note:

If the exception occurs, PHP code after the statement will not be executed so that used try-catch block.

Throw

Throw statement is used to throw the new exception that require the single argument.

Syntax

throw new Exception;
you can also pass the custom  error message to the exception object

throw new Exception( “problem occur” );

Example with exception

function Fun($number) 
{
return $number;
}
  
try {
   $a = Fun(200);
  if($a > 80);
  throw new Exception("Value must be less than 80");
  
  
    if($a < 100);
  throw new Exception("Value must be greator than 100");
  
  
}


catch(Exception $e) {
  echo 'Message: ' .$e->getMessage();
}

Output

Message: Value must be less than 80

Multiple exception

<?php

function Fun($number) 
{
return $number;
}
  
try {
   $a = Fun(200);
  if($a > 80);
  throw new Exception("Value must be less than 80");
  
  
    if($a < 100);
  throw new Exception("Value must be greater than 100");
  
  
}


catch(Exception $e) {
  echo 'Message: ' .$e->getMessage();
}

?>

Output

Message: Value must be less than 80

If you want to learn more about error please visit
php tutorial



How to use HTML and CSS into the PHP

How to use HTML and CSS into the PHP.

HTML with PHP

In this example, I am going to tell you, how to use HTML and CSS in your PHP script and make your web page more attractive.

In below example, we are using HTML and CSS in PHP script and print employs details in an effective manner. We are using the table in a PHP script and print serial number employees’ name and employs id.

You can also use this table and CSS at the time of retrieving the data from the database.

Syntax

<html>
<head>
<title> Employ name and ID</title>
<style>
th { text - align: left; background: #E6E6E6; }
th, td { padding:10px; }
tr.alt td { background: #EAEAEA; }
</style>
</head>
<body>
<h2> Print employ id in table using php</h2>
<table cellspacing="0" border="0" style="width: 20em; border: 1px solid
#666;">
<tr>
<th> Serial number  </th>
<th> Name  </th>
<th>Employ ID</th>
</tr>
<?php
$number_of_emp = 10;
$uk="pT-";
for ( $i=1; $i <= $number_of_emp; $i++ )
{
$num2= rand(1000,10000);
$num1 = $uk.$num2 ;
?>
<tr <?php if ( $i % 2 != 0 ) echo ' class="alt"' ?>>
<td> P <sub> <?php echo $i?> </sub> </td>
<td> <?php echo "Umar";?> </td>
<td> <?php echo $num1?> </td>
</tr>
<?php
}
?>
</table>
</body>
</html>


Explanation of PHP code

$number_of_emp is a simple integer type variable that holds the maximum number of employs limit after that create a random number and con concatenate with string for uniquely employs ID and assign to the $num1 variable.

Line number 5 decide the style of the table rows, if the number is even (mean number of rows) then the class “ALT” is called otherwise simply print without any style.

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