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

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.


Bookmark and Share

Convert a three digit integer to an array in php

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 ) 


Bookmark and Share

cURL Simple Example in PHP

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);     
?>


Bookmark and Share

What is cURL in PHP?

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.


Bookmark and Share

Difference between Cookies and Session in PHP

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.


Bookmark and Share

Difference between Get and post method

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".


Bookmark and Share

How many ways you can delete a session variable?

How many ways you can delete a session variable?

In order to kill the session variable use:
  1. unset($_SESSION["test"]);
  2. session_destroy() — Destroys all data registered to a session
  3. session_unregister() — Unregister a global variable from the current session.
  4. session_unset() — Free all session variables.
To use the session variables again, session_start() has to be called.


Bookmark and Share

Difference between array_slice and array_splice in php

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() 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
    
)


Bookmark and Share

How To Remove Html Tags from String in Php

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);


Bookmark and Share

Difference between include and include_once in php

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"); 
?>


Bookmark and Share

Difference between php5 and php6

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.


Bookmark and Share

Difference between php4 and php5


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().


Bookmark and Share

Difference between mysql4 and mysql5

Difference between mysql4 and mysql5 

MySQL 5 introduced new important features
1. Cursors
2. Triggers
3. Stored Procedures
4. Views


Bookmark and Share

How to delete file in PHP

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


Bookmark and Share

Difference between mysql_fetch_object and mysql_fetch_array in PHP

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


Bookmark and Share

chdir function in php


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


Bookmark and Share

Difference between join and implode in php.


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.


Bookmark and Share

Check password length in php


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!";
     }



?>


Bookmark and Share

Regular expression Basics in php

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


Bookmark and Share

.htaccess file and URL rewrite rule in php


.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      


Bookmark and Share