Entries by admin

is_dir()

May 13, 2011 | Filed Under Php File System, PHP Tutorial | Leave a Comment

This function checks whether the specified file is a directory.

Syntax: is_dir(file)

Description:

The Parameter “file” is Required. It Specifies the file to be checked.

This function returns TRUE if the directory exists.

Example1:
Code:

$file = "sadique";
if(is_dir($file))
    {
  echo ("$file is a directory");
  }
else
  {
  echo ("$file is not a  directory");
  }

Output:

sadique is not a directory

Example2:
Code:

$file = "C:";
if(is_dir($file))
  {
  echo ("$file is a directory");
  }
else
  {
    echo ("$file is not a directory");
  }

Output:

C: is a directory

Article written by admin

unlink()

May 13, 2011 | Filed Under Php File System, PHP Tutorial | Leave a Comment

This function deletes a file.

Syntax: unlink(filename,context).

Description:

The first “Parameter” filename is Required. It Specifies the file to be deleted.

The second Parameter “context” is Optional. It Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream .

This function returns TRUE on success, or FALSE on failure.

Example:
Code:

$file  = "test.txt";
if (!unlink($file))
  {
  echo ("Error in the deleting $file");
  }
else
  {
  echo ("Deleted $file");
     }

Output:

Deleted test.txt

Explanation:

The above code will delete the test.txt file.

Article written by admin

basename()

May 13, 2011 | Filed Under Php File System, PHP Tutorial | Leave a Comment

This function returns a filename without the path.

Syntax: basename ( path ,suffix )

The first Parameter “path” is Required. It Specifies the path to be check.

The second Parameter “suffix” is Optional.It Specifies a file extension. If the filename has this file extension, the file extension will not show

Strips a filename from path and suffix.

The basename() function returns a filename without the path. If the suffix parameter is provided, it will also be removed.

Example:
Code:

$filename = "D:\wamp\www\sadique\session_name.php";
echo "$filename";
echo basename($filename) . "";
echo basename($filename, ".php") . "";
echo basename($filename, "_name.php");

Output:

D:\wamp\www\sadique\session_name.php
session_name.php
session_name
session

Explanation:

Explanation:

The above code shows how basename() can be used to modify a filename.

Article written by admin

Object Interfaces

May 9, 2011 | Filed Under OOPS, PHP Tutorial | Leave a Comment

Interface is a class with no data members and contains only member functions and they lack its implementation. Any class that inherits from an interface must implement the missing member function body. In PHP 5 class may inherit only one class, but because interfaces lack an implementation any number of class can be inherited. In PHP 5, interfaces may declare only methods. An interface cannot declare any variables. To extend from an Interface, keyword implements is used. PHP5 supports class extending more than one interface.Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.

Example:
Code:

interface employee
{
	function setdata($empname,$empid);
	function getData();
}

class Salary implements employee
{
	function   setdata($empname,$empid)
	{
        $this->empname=$empname;
	   $this->empid=$empid;
	}

	function getData()
	{
	    echo "The employee name is: ",$this->empname,"
"; echo "The employee Id is: ",$this->empid,"
"; echo "Inside Salary Class"; } } $a = new Salary(); $a->setData("Cruise",101); $a->getData();

Output:

The employee name is: Cruise
The employee Id is: 101
Inside Salary Class

Article written by admin

extends

May 9, 2011 | Filed Under OOPS, PHP Tutorial | Leave a Comment

When you need classes with similar variables and functions to another existing class. The extended or derived class has all variables and functions of the base class,and what you add in the extended definition. It is not possible to subtract from a class. An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword ‘extends’.

This is also called a “parent-child” relationship. You can create a class, parent, and use extends to create a new class based on the parent class: the child class. You can even use this new child class and create another class based on this child class.

Example:
Code:

class person
{

var $age;
function set_age($age)
{

$this->age=$age;
}
function  get_age()
{
 echo "The age is : " , $this->age ,"
"; } } class student extends person { var $name; function set_name($name) { $this->name=$name; } function get_name() { echo "The name is : " , $this->name; } } $obj=new student; $obj->set_age(23); $obj->get_age(); $obj->set_name("Andy"); $obj->get_name();

Output:

The age is : 23
The name is : Andy

Explanation:

This student that has all variables and functions of person plus an additional variable $name and an additional function set_name() and get_name(). You create a student the usual way and can now set and get the person age. You can still use student functions on student.

Article written by admin

class_exists()

May 9, 2011 | Filed Under OOPS, PHP Tutorial | Leave a Comment

This function Checks if the class has been defined.

Syntax: class_exists ( string class_name , bool autoload )

Description:

This function returns TRUE if the class_name has been defined, FALSE otherwise.It will attempt to call __autoload by default, if you don’t want class_exists() to call __autoload, you can set the parameter autoload to FALSE.

Example:
Code:

class myclass
{
var $name;
function set_name($name)
{
$this->name=$name;
}

function get_name()
{
echo $this->name;
}

}
if(class_exists('myclass'))
{
$obj=new myclass;
$obj->set_name('sadique');
$obj->get_name();
}
else
{
echo "myclass class is not exist.";
}

Output:

Swan

Article written by admin

method_exists()

May 9, 2011 | Filed Under OOPS, PHP Tutorial | Leave a Comment

This function Checks if the class method exists.

Syntax: method_exists ( object, string method_name )

Description:

This function returns TRUE if the method_name has been defined for the given object, FALSE otherwise.

Example:
Code:

class calculate
{
function add()
{}

}

$obj=new calculate;
if(method_exists($obj,add))
{
echo "add function is exist";

}
else
{
echo "add function is not exist";
}

Output:

add function is exist

Article written by admin

property_exists()

May 9, 2011 | Filed Under OOPS, PHP Tutorial | Leave a Comment

This function Checks if the object or class has a property.

Syntax: property_exists ( mixed class, string property )

Description:

This function checks if the given property exists in the specified class and if it is accessible from the current scope.

The first Parameter “class” is Required.It specifies a string with the class name or an object of the class to test for.

The second parameter “property” is Required.It specifies the name of the property.

It returns TRUE if the property exists, FALSE if it doesn’t exist or NULL in case of an error occur.

Example:
Code:


class myClass {
    var $name;

}

$obj=new myClass;
if(property_exists('myClass','name'))
{
echo "name property is exist";
}
else
{
echo "name property  is not exist";
}

Output:

name property is exist

Article written by admin

session_regenerate_id

May 7, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

This function replaces the current session id with a newly generated one .

Syntax: session_regenerate_id()

Description:

session_regenerate_id() will replace the current session id with a new one, and keep the current session information.

Example:
Code:

session_start();

$old_sessionid = session_id();

session_regenerate_id();

$new_sessionid   session_id();

echo  "Old Session: $old_sessionid";
echo "New Session: $new_sessionid";

Explanation:

Article written by admin

Delete a Cookie

May 7, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

By default, the cookies are set to be deleted when the browser is closed. We can override that default by setting a time for the cookie’s expiration but there may be occasions when you need to delete a cookie before the user closes his browser, and before its expiration time arrives. To do so, you should assure that the expiration date is in the past. To destroy the cookie, simply use setcookie.

Example:
Code:

// set the expiration date   to one hour  ago
setcookie("lastVisit",   "date("G:i - m/d/y")", time()-3600);

Explanation:

It sets expiration time 1 hour ago.

Article written by admin

$_COOKIE()

May 7, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

The PHP $_COOKIE variable is used to retrieve a cookie value.

Syntax: $_COOKIE['cookie name']

Description:

The parameter “cookie name” is Required.

Example:
Code:

if(isset($_COOKIE['lastVisit']))
   {
 $last = $_COOKIE['lastVisit'];
 echo "You last visited on ". $last;
 }
 else
 {
 echo "Welcome to our site!";
 }

Explanation:

This code first checks if the cookie exists. If it does, it tells them when they last visited. If they are new, it skips this and prints a welcome message.

Article written by admin

setcookie()

May 7, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

This function is used to set a cookie.

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Syntax: setcookie(name, value, expire, path, domain,secure,httponly).

Description:

When you create a cookie,using the function setcookie,the parameters name, value, expire, path, domain are used.All the parameters except the name argument are optional.

The first parameter “name” is Required. It specifies the name of your cookie. You will use this name to later retrieve your cookie, so don’t forget it!

The second parameter “value” is Optional. It specifies the value that is stored in your cookie. Common values are username(string) and last visit(date).

The third parameter “expiration” is Optional The date when the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and be removed when the browser is restarted.

The fourth parameter “path” is Optional. It specifies on the server in which the cookie will be available on. If set to ‘/’, the cookie will be available within the entire domain. If set to ‘/example/’, the cookie will only be available within the /example/ directory and all sub-directories such as /example/examp/ of domain. The default value is the current directory that the cookie is being set in.
domain.

The fifth parameter “domain” is Optional.It specifies that the cookie is available to. To make the cookie available on all subdomains of example.com (including example.com itself) then you’d set it to ‘.example.com’. Although some browsers will accept cookies without the initial ., ยป RFC 2109 requires it to be included. Setting the domain to ‘www.example.com’ or ‘.www.example.com’ will make the cookie only available in the www subdomain.

The sixth parameter “secure” is Optional.It specifies that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists. On the server-side, it’s on the programmer to send this kind of cookie only on secure connection (e.g. with respect to $_SERVER["HTTPS"]).

The seventh parameter “httponly” is Optional.It specifies When TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won’t be accessible by scripting languages, such as JavaScript. This setting can effectively help to reduce identity theft through XSS attacks (although it is not supported by all browsers). Added in PHP 5.2.0. TRUE or FALSE.

Example:
Code:

//Calculate 30 days in the future
//seconds * minutes * hours * days + current time
$inOneMonth = 60 * 60 * 24 * 30 + time();
setcookie('lastVisit', date("G:i - m/d/y"), $inOneMonth);

Explanation:

In the above example we have created a cookie that stores the user’s last visit. We want to ignore people that take longer than one month to return to the site, so we have set the cookie’s expiration date to one month in the future!

Article written by admin

session_unregister()

May 5, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

This function Unregister a global variable from the current session.

Syntax: session_unregister ( string name )

Description:

The parameter “string name” is specified to be unregistered.

This function unregisters a session variable registered with session_register(). If $_SESSION was used to register the session variable, use unset() to unregister it.

Example:
Code:

session_start();
session_register("session_variable");
$session_variable   = "xyz123";
print "session_variable: " . $session_variable;
session_unregister("session_variable");
Article written by admin

session_register()

May 5, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

This function register one or more global variables with the current session.

Syntax: session_register ( mixed name [, mixed ...] )

Description:

session_register() accepts a variable number of arguments, For each name, session_register() registers the global variable with that name in the current session. It does not assign a value to the session variable.that this function takes the name of a variable as argument, not the variable itself. You can use session_unregister() to remove variables from the session, for example, when the user removes a product item from the shopping cart.

Example:
Code:

session_start();
session_register("session_variable");
$session_variable = "xyz123";
print "session_variable: " . $session_variable;
session_unregister("session_variable");

Output:

session_variable: xyz123

Article written by admin

session_unset()

May 5, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

It free all session variables.

session_unset ( )

Description:

The unset() function is used to free the specified session variable.

Example:
Code:


 // you  have to open the session to be able to modify or remove it
 session_start(); 

 // or this would remove all the variable in the session
 session_unset(); 

Explanation:

This function would remove all the variable in the session.

Article written by admin

session_id()

May 5, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

It Get or set the current session id.

Syntax: session_id (id )

Description:

The parameter “id” is Optional. If id is specified, it will replace the current session id.

It returns the session ID created with session_start(). It can also be used to change the session ID.

Example:
Code:

session_id();
session_start();
print  "My session ID is: " . session_id();
session_destroy();

Output:

My session ID is: balv2nv98i27j5drl7c720mjs7

Article written by admin

session_destroy()

May 5, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

It destroys all stored data registered with the current session.

Syntax: session_destroy()

Description:

This function will reset your session and you will lose all your stored session data.

Example:
Code:


 session_start(); 

 // destroy the session
 session_destroy();
 
Article written by admin

session_cache_limiter()

May 5, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

It Get or set the current cache limiter.

Syntax: session_cache_limiter ( string cache_limiter )

Description:

It returns the name of the current cache limiter. If cache_limiter is specified, the name of the current cache limiter is changed to the new value.

The cache limiter defines which cache control HTTP headers are sent to the client. These headers determine the rules by which the page content may be cached by the client and intermediate proxies. Setting the cache limiter to nocache disallows any client/proxy caching. A value of public permits caching by proxies and the client, whereas private disallows caching by proxies and permits the client to cache the contents.

Example:
Code:


/* set the cache limiter to 'private' */

session_cache_limiter('private');
$cache_limit = session_cache_limiter();

echo "The cache limit is set to $cache_limit";

Output:

The cache limit is set to private

Article written by admin

session_cache_expire

May 5, 2011 | Filed Under PHP Tutorial, Sessions and Cookies | Leave a Comment

It returns current cache expire.

Syntax: session_cache_expire ($new_cache_expire)

Description:

The parameter “$new_cache_expire” is given, the current cache expire is replaced with new_cache_expire.

It returns the current setting of session.cache_expire. The value returned should be read in minutes, defaults to 180.

Example:
Code:


/* set the cache limiter to 'private' */

session_cache_limiter('private');
$cache_limiter  = session_cache_limiter();

/*   set the cache expire to 40 minutes */
session_cache_expire(40);
$cache_expire  = session_cache_expire();

/* start the session */

session_start();

echo "The cache is set to $cache_limiter";
echo "The cached session pages expire after $cache_expire minutes";

Output:

The cache is set to private
The cached session pages expire after 40 minutes

Article written by admin

$_COOKIE

May 5, 2011 | Filed Under PHP Tutorial, Super Global variables | Leave a Comment

A cookie is used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Example:
Code:

$expire=time()+60*60*24*30;
setcookie("user", "Watson", $expire);
// Print a cookie
echo $_COOKIE["user"];

Output:

Watson

Explanation:

The first time the page is loaded the cookie hasn’t been created . When the page is reloaded the value is printed.

Article written by admin

© PHPInterviewQuestion.com 2009 - 2012

eXTReMe Tracker