Category Archives: Php Interview Questions

Php Force to download a file


Php Force to download a file : Sometimes we need to download file forcefully. Here in this tutorial we are going to explain how you can force browser to download the files.


Php Force to download a file

You can use below method to download a file in php as below –

Php Force to download a file Example:

$file = "http://testdomain.com/file.exe"; 
header("Content-Description: File Transfer"); 
header("Content-Type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=\"$file\""); 
readfile ($file); 

The above example will download the file from the specified location.

PHP convert string to number


PHP convert string to number : We sometimes need to convert the string to integer. There are many ways to convert string to number. Here in this tutorial we are going to explain how you can convert a string to number forcefully using different methods. We will also explain how to convert string to float etc.


PHP convert string to number – Interger Example

If you have string like – “33.3” and you want to convert it to integer use the type cast as below –

PHP convert string to number – integer example:

$number = "33.3";
$int = (int)$number;

In this example $number variable contains the string with “33.3”. Double quotes makes it string so we have used type cast int to convert it to integer.


More About String to Number

Let us learn more about conversion –


PHP convert string to Integer : inval() function Example-

You can also convert string to integer using the function intval. Here is an example of intval-

PHP convert string to integer example:

$number = "33.3";
$int = intval($number);

The above function takes the string and returns the integer.

Now let us have some example about the float conversion.

PHP convert string to float Method 1

You can also convert string to float using the type cast (double) as below –

PHP convert string to float example:

$number = "33.3";
$int = (double)$number; // will give 33.3
or 
$int = (float)$number; // will give 33.3

The double type cast will convert the number in float.

PHP convert string to float Method 2 : floatval() Function Example

You can also convert string to float using the floatval() function as below –

PHP convert string to float example:

$number = "33.3";
$int = floatval($number); // will give 33.3

The floatval() takes string and converts it to the float as above.

PHP Set HTTP header to UTF-8


PHP Set HTTP header to UTF-8: Sometimes we need to set the HTTp header to UTF-8 in php. header() is used to set the HTTp header to UTF-8. The header() sends a raw HTTP header. In this tutorial we are going to explain how you can set the HTTP header to UTF-8 using PHP.


PHP Set HTTP header to UTF-8

Use the header() function and add the charset utf-8 as below –

PHP Set HTTP header to UTF-8:

header('Content-Type: text/html; charset=utf-8');

Now call this before sending any output to the client. Add this at the starting of the page. There should be no blank space before it because it may cause error. You can check the headers already sent or not using the function headers_sent(). For the function headers_sent() you can refer – http://php.net/manual/en/function.headers-sent.php

Refresh page using PHP


Refresh page using PHP Function : Sometimes in php we need to reaload ie. refresh a page. There can be several ways to refresh a page in php we are going to explain few of them. In this tutorial we will explain how you can refresh a page after some interval of time.


Refresh page using PHP

You can use the header() function to refresh the page here is an example to refresh the page using this function-

Refresh page using PHP function:

$second = 10;
header("Refresh:$second");

This will refresh page after each 10 seconds.

Refresh page using PHP And Redirect to another page-

If you to redirect to another page after refreshing the page you can add the second parameter ie. page you want to redirect-

Refresh page using PHP And Redirect to another page-:

$second = 10;
header("Refresh:$second; url=testpage.php" );

This will refresh the page after the given time and redirect to the given page.

More About PHP Refresh Page

Let us have look over another methods which we can use to refresh the page in php.

Refresh page in PHP using HTML META

You can also refresh a page using the HTML META as below-

Refresh page in PHP using HTML META Example

echo("");

Refresh page in PHP using Javascript

You can also refresh a page using the javascript as below-

Refresh page in PHP using Javascript Example




You can also use the javascript to refresh the page in php. The above example will reload the same page after refreshing.

Conversation between two time zone

Conversation between two time zone :
A simple method for Conversation between two timezone is as below :

Conversation between two time zone

format('U'));
?>

You can change the timezone as per your country.

php default timezone india

php default timezone india :
You can set default timezone india as below :

Set default timezone india

date_default_timezone_set("Asia/Kolkata"); 
$date = date('Y-m-d H:i:s');

Set default timezone in php

Set default timezone in php : date_default_timezone_set() sets the default timezone for all the date functions which
are used in the script.

A simple example to set default timezone in php is as :


date_default_timezone_set('America/Los_Angeles');
$date = date('Y-m-d H:i:s');

Which will return the current date as per the defalt timezone set.

How to get current date and time in php

How to get current date and time in php ?
You can simply use date function to get current date in php as below :


$date = date('Y-m-d H:i:s');

This would return the the current date in format of $date = ‘2015-05-06 16:30:01’.

In php 5.2.0 you can also use this method to get current date and time in php.

$date = new DateTime();
$currDate = $date->format('Y-m-d H:i:s');

This would also return the the current date in format of $currDate = ‘2015-05-06 16:30:01’.

For Setting Default timezone in php you can refer this article :
http://tutorialsplane.com/set-default-timezone-in-php/

Late Static Binding In Php Example

Late Static Binding In Php Example

Late Static binding feature was introduced in php 5.3.0. This is used to reference the called class in the context of static inheritance. This is used to solve the limitation of inheritance.

Example of Late Static Binding in PHP.