Showing posts with label PHP interview questions and answers. Show all posts
Showing posts with label PHP interview questions and answers. Show all posts
How can get current year in php
Posted by Raj
How can get current year in php.
In this article,I will show how to get Current Year in PHP.
The PHP Date() Function:
The PHP date() function formats a date and time.
Syntax
date(format,timestamp)
d - Represents the day of the month (01 to 31)
m - Represents a month (01 to 12)
Y - Represents a year (e.g.2012)
Example:
<?php
echo $date=date('Y');//Get Current Year in PHP
?>
Output:
2012.
In this article,I will show how to get Current Year in PHP.
The PHP Date() Function:
The PHP date() function formats a date and time.
Syntax
date(format,timestamp)
d - Represents the day of the month (01 to 31)
m - Represents a month (01 to 12)
Y - Represents a year (e.g.2012)
Example:
<?php
echo $date=date('Y');//Get Current Year in PHP
?>
Output:
2012.
Convert a three digit integer to an array in php
Posted by Raj
How do I convert a three digit integer to an array so that each digit is in its own element?
In this article, I will show how to convert a three digit integer to an array so that each digit is in its own element.
we are using php str_split() Function.
PHP str_split() function :
The str_split() function splits a string into an array.
Syntax
str_split(string,length)
Example:
<?php
$string="123";
$arr=str_split($string);
print_r($arr);
?>
Output: Array ( [0] => 1 [1] => 2 [2] => 3 )
In this article, I will show how to convert a three digit integer to an array so that each digit is in its own element.
we are using php str_split() Function.
PHP str_split() function :
The str_split() function splits a string into an array.
Syntax
str_split(string,length)
Example:
<?php
$string="123";
$arr=str_split($string);
print_r($arr);
?>
Output: Array ( [0] => 1 [1] => 2 [2] => 3 )
cURL Simple Example in PHP
Posted by Raj
cURL Simple Example in PHP:
cURL allows you to connect and communicate to many different types of servers with many different types of protocols.
PHP Supports libcurl.
Example:
<?php
// initialize curl session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "example.com"); // set url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//return the transfer as a string
$output = curl_exec($ch);//output string
// close curl session
curl_close($ch);
?>
cURL allows you to connect and communicate to many different types of servers with many different types of protocols.
PHP Supports libcurl.
Example:
<?php
// initialize curl session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "example.com"); // set url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//return the transfer as a string
$output = curl_exec($ch);//output string
// close curl session
curl_close($ch);
?>
What is cURL in PHP?
Posted by Raj
What is cURL in PHP?
cURL allows you to connect and communicate to many different types of servers with many different types of protocols.
PHP Supports libcurl.
Requirements:
In order to use PHP's cURL functions you need to install the libcurl package.PHP 5.0.0 requires a libcurl version 7.10.5 or greater.
cURL Functions:
curl_close : Close a cURL session.
curl_errno: Return the last error number.
curl_error: Return a string containing the last error for the current session.
curl_exec: Perform a cURL session.
curl_getinfo: Get information regarding a specific transfer.
curl_init: Initialize a cURL session.
curl_setopt_array: Set multiple options for a cURL transfer.
curl_setopt: Set an option for a cURL transfer.
curl_version: Gets cURL version information.
cURL allows you to connect and communicate to many different types of servers with many different types of protocols.
PHP Supports libcurl.
Requirements:
In order to use PHP's cURL functions you need to install the libcurl package.PHP 5.0.0 requires a libcurl version 7.10.5 or greater.
cURL Functions:
curl_close : Close a cURL session.
curl_errno: Return the last error number.
curl_error: Return a string containing the last error for the current session.
curl_exec: Perform a cURL session.
curl_getinfo: Get information regarding a specific transfer.
curl_init: Initialize a cURL session.
curl_setopt_array: Set multiple options for a cURL transfer.
curl_setopt: Set an option for a cURL transfer.
curl_version: Gets cURL version information.
Difference between Cookies and Session in PHP
Posted by Raj
Difference between Cookies and Session in PHP
- Cookies are stored in the user’s browser memory.A cookie can keep information in the user's browser until deleted.
- Session : logical object that allows you to preserve data across subsequent http requests.Whenever PHP creates a new session, it generates a session ID.
- Session values are store in server side not in user’s machine. A session is available as long as the browser is opened. User couldn't be disabled the session.
- Cookies are stored in client side and sessions are stored in server side.
- Cookies can only store strings.We could store not only strings but also objects in session.
- we could be save cookie for future reference, but session couldn't. When users close their browser, they also lost the session.
Difference between Get and post method
Posted by Raj
What is the difference between Get and post method.
- Get method is not secure as data will be appeared in the url address.
- Using post method is much secure as it will not appear in the url address.
- Get Method transfers only 255 char. No limitation for the Post method
- Get request is comparatively faster.
- Get is the default method of the HTML From elememt.we need to specify the method attribute and give it the value "POST".
- If you use POST method than all the form element value is passed from the header response. which we can get it using the $_POST method.
- If you use GET method ,we can get it using the $_GET method.
- You can only bookmark if the method used is GET and cannot bookmark in POST method.
- GET request is sent via URL, so GET can carry only text data whereas POST has no such restriction and it can carry both text as well as binary data.
- GET requests are restricted to use ASCII characters only whereas POST requests can use the 'enctype' attribute with a value "multipart/form-data".
How many ways you can delete a session variable?
Posted by Raj
How many ways you can delete a session variable?
In order to kill the session variable use:
- unset($_SESSION["test"]);
- session_destroy() — Destroys all data registered to a session
- session_unregister() — Unregister a global variable from the current session.
- session_unset() — Free all session variables.
Difference between array_slice and array_splice in php
Posted by Raj
Difference between array_slice() and array_splice() in php
array_splice() is used to remove some elements from an array and replace with specified elements.
array_slice() is used to extract a selected parts of an array .
array_slice() Example:
<?php
$numbers = array("1", "2", "3", "4", "5");
$new_numbers = array_slice($numbers, 2);
$new_numbers = array_slice($numbers, -3, 1);
?>
Output:
Array
(
[0] => 3
[1] => 4
[2] => 5
)
Array
(
[0] => 3
)
array_splice() is used to remove some elements from an array and replace with specified elements.
array_slice() is used to extract a selected parts of an array .
array_slice() Function in PHP:
array_slice() is used to extract a selected parts of an array .array_slice() Example:
<?php
$numbers = array("1", "2", "3", "4", "5");
$new_numbers = array_slice($numbers, 2);
$new_numbers = array_slice($numbers, -3, 1);
?>
Output:
Array
(
[0] => 3
[1] => 4
[2] => 5
)
Array
(
[0] => 3
)
How To Remove Html Tags from String in Php
Posted by Raj
How To Remove Html Tags From String In Php
In this article,I will explain how to remove Html tags from string with php.In php we are using strip_tags function to strip HTML and php tags.
strip_tags function returns a string with HTML and PHP tags stripped from a given string.
strip_tags Syntax:
strip_tags(string $string,string $allowable_tags);
In this article,I will explain how to remove Html tags from string with php.In php we are using strip_tags function to strip HTML and php tags.
strip_tags function returns a string with HTML and PHP tags stripped from a given string.
strip_tags Syntax:
strip_tags(string $string,string $allowable_tags);
Difference between include and include_once in php
Posted by Raj
Difference between include and include_once in php
All these functions include and include_once are used to include the files in the php page.
PHP include() Function:
If the specified file is not available ,the include() function generates a warning,and executes the
remaining code.
Example:
<?php
include("test.php");
?>
All these functions include and include_once are used to include the files in the php page.
PHP include() Function:
If the specified file is not available ,the include() function generates a warning,and executes the
remaining code.
Example:
<?php
include("test.php");
?>
Difference between php5 and php6
Posted by Raj
Difference between php5 and php6
In this article, I will explain about upcoming changes in php6.php6 is not yet released.PHP6 will offer many improvements and will remove some of the functionality that has been in older versions of PHP.
In this article, I will explain about upcoming changes in php6.php6 is not yet released.PHP6 will offer many improvements and will remove some of the functionality that has been in older versions of PHP.
Difference between php4 and php5
Posted by Raj
Difference between php4 and php5
PHP5 introduces many new features, I have mentioned some of them:
Unified Constructors and Destructors:
In PHP4, constructors had same name as the class name. In PHP5, you have to name your constructors as __construct() and destructors as __destruct().
Difference between mysql4 and mysql5
Posted by Raj
Difference between mysql4 and mysql5
MySQL 5 introduced new important features
1. Cursors
2. Triggers
3. Stored Procedures
4. Views
MySQL 5 introduced new important features
1. Cursors
2. Triggers
3. Stored Procedures
4. Views
How to delete file in PHP
Posted by Raj
How to delete file in PHP:
In this article,I will explain how to delete file using php unlink function with sample example.
unlink: Deletes a file
In this article,I will explain how to delete file using php unlink function with sample example.
unlink: Deletes a file
Difference between mysql_fetch_object and mysql_fetch_array in PHP
Posted by Raj
difference between mysql_fetch_object and mysql_fetch_array in PHP
mysql_fetch_object :
mysql_fetch_object returns the results from database as objects.(fetches a result row as object)
$result->email
mysql_fetch_object :
mysql_fetch_object returns the results from database as objects.(fetches a result row as object)
$result->email
chdir function in php
Posted by Raj
chdir function in php
The chdir() function changes the current directory to the other directory.
chdir() Syntax:
chdir(string $directory)
chdir example:
<?php
getcwd();//current directory (e.g.c:\project)
chdir("files");//changes to files directory
echo $current_directory=getcwd();
?>
Output: C:\project\files
Difference between join and implode in php.
Posted by Raj
Difference between join and implode in php.
In this example, I will show how to use join and implode in php.
Join : Join is an Alias of implode().
Example:
<?php
$arr = array('Rajesh', 'India', 'Tutorials');
$str = join(",", $arr);
echo $str;
?>
Output: Rajesh,India,Tutorials.
implode: implode Returns a string from array elements.
Example:
<?php
$arr = array('Rajesh', 'India', 'Tutorials');
$str = implode(",", $arr);
echo $str;
?>
Output:Rajesh,India,Tutorials.
Check password length in php
Posted by Raj
Check password length in php
I have written function to check password length in php
<?php
$password="checkpassword";
$passwordCheck=passwordLength($passwod);
function passwordLength($pwd)
{
$pwdlen=strlen($pwd);//calculate length of password using strlen function
if($pwdlen<=4) //if length of password is less than 4 then password is too short.
{
return "Password too short!";
}
else if ($pwdlen>16) //if length of password is greater than 16 then password is too Long.
{
return "Password too Long!";
}
}
?>
Regular expression Basics in php
Posted by Raj
Regular expression Basics in php:
In this article ,i will show how to use regular expressions in php.
\char escape char
. Any single character
[chars] One of following chars
[^chars] None of following chars
text1|text2 text1 or text2
? 0 or 1 of the preceding text
* 0 or N of the preceding text (hungry)
+ 1 or N of the preceding text
Example: (.+)\.html? matches test.htm and test.html
(text) Grouping of text
Example: ^(.*)\.html foo.php?bar=$1
^ Start of line anchor
$ End of line anchor
Example: ^test(.*) matches test and test1 but not ttest
(.*)1$ matches a1 and b1, but not test
In this article ,i will show how to use regular expressions in php.
\char escape char
. Any single character
[chars] One of following chars
[^chars] None of following chars
text1|text2 text1 or text2
? 0 or 1 of the preceding text
* 0 or N of the preceding text (hungry)
+ 1 or N of the preceding text
Example: (.+)\.html? matches test.htm and test.html
(text) Grouping of text
Example: ^(.*)\.html foo.php?bar=$1
^ Start of line anchor
$ End of line anchor
Example: ^test(.*) matches test and test1 but not ttest
(.*)1$ matches a1 and b1, but not test
.htaccess file and URL rewrite rule in php
Posted by Raj
.htaccess file and URL rewrite rule in php
Redirect URLs using .htaccess :
You can use .htaccess to redirect users to a different URL(redirect a url)
Sometimes you need to redirect some URL and/or page on your site to another one.
using the “Redirect” directive:
Redirect /folder http://www.example.com/newfolder
Another useful directive is the RedirectMatch
RedirectMatch "\.html$" http://www.example.com/index.php
This will redirect all requests to files that end with .html to the index.php file.
One of the more powerful tricks of the .htaccess hacker is the ability to rewrite URLs
simple rewriting
all requests to whatever.htm will be sent to whatever.php:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [NC]
RewriteRule ^menu.html menu.php