what is difference between “$a dollars” or “{$a} dollars” ?

January 15, 2010 | Filed Under PHP | Comments Off

Best way to understand the problem is example.

<?php

$a=’Hello’;

Echo “$aworld” // blank because it treat as variable

Echo “{$a}world” // Helloworld

?>

Article written by admin

what is __toString magic function in php?

January 10, 2010 | Filed Under PHP | Comments Off

The __toString method allows a class to decide how it will react when it is converted to a string.

style="color: #000000;"><?php
// Declare a simple class
class TestClass
{
public
$foo;

public function __construct($foo) {
$this->foo = $foo;
}

public function __toString style=”color: #007700;”>() {
return
$this->foo;
}
}

$class = new TestClass(‘Hello’);
echo
$class;
?>

The above example will output:

Hello
Article written by admin

Magic Functions in php?

January 10, 2010 | Filed Under PHP | Comments Off

These are functions in PHP.

1__construct

2-__destruct (see Constructors and Destructors),

3-__call

4-__get

3-__set

4-__isset

5-__unset (see Overloading)

6-__wakeup

7-__toString

8-__set_state

9-__clone

Article written by admin

What’s __sleep and __wakeup in PHP?

January 10, 2010 | Filed Under PHP | Comments Off

These are  the magic function in php.
__sleep
serialize() checks if your class has a function with the magic name __sleep. If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn’t return anything then NULL is serialized and E_NOTICE is issued.
__wakeup
Conversely, unserialize() checks for the presence of a function with the magic name __wakeup. If present, this function can reconstruct any resources that the object may have.

server = $server;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->connect();
}

private function connect()
{
$this->link = mysql_connect($this->server, $this->username, $this->password);
mysql_select_db($this->db, $this->link);
}

public function __sleep()
{
return array(‘server’, ‘username’, ‘password’, ‘db’);
}

public function __wakeup()
{
$this->connect();
}
}
?>

Article written by admin

How do you call a constructor for a parent class?

January 10, 2010 | Filed Under PHP | Comments Off

parent::constructor($value)

Article written by admin

WHAT ARE THE DIFFERENT TYPES OF ERRORS IN PHP?

January 10, 2010 | Filed Under PHP | Comments Off

There are three type of errors are present in PHP

1-Notices
2-Warnings
3-Fatal errors

Notices:
These are trivial, non-critical errors. that does not terminate script .
Condition:
1- Accessing a variable that not define.

Warnings:
These are more serious errors
Condition:
1-attempting to include() a file which does not exist.

Fatal errors:
These are critical errors that terminate script and stop
Condition:
1-instantiating an object of a non-existent class
2- Calling a  non-existent function
3-Missing semicolon
4-missing braces
5- Destroyed DOM

Article written by admin

Differences between DROP a table and TRUNCATE a table?

January 10, 2010 | Filed Under PHP | Comments Off

DROP: This will delete the table structure and its data.

syntax-DROP TABLE table_name

TRUNCATE: This will delete the table  data but not its structure.
syntax-TRUNCATE TABLE table_name

Article written by admin

What is difference between echo $a or echo {$a} ?

January 10, 2010 | Filed Under PHP | Comments Off

Best way to understand the problem is example.

<?php

$a=’Hello’;

Echo “$aworld” // blank because it treat as variable

Echo “{$a}world” // Helloworld

?>

Article written by admin

How many number of tables are present in MYSQL Data base?

January 10, 2010 | Filed Under MySQL, PHP | Comments Off

Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM

Article written by admin

How can I execute a PHP script using command line?

January 10, 2010 | Filed Under PHP | Comments Off

Just run the PHP CLI (Command Line Interface) program and provide the PHP script file  name as the command line argument. For example, “php  myScript.php”, assuming “php” is the command to invoke the CLI program.
Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.

Article written by admin

What is difference between Persistent Cookie and Temporary cookies?

January 10, 2010 | Filed Under PHP | 2 Comments

Temporary cookies :

By Default Temporary cookie is active which is store in browser memory, live until browser  is closed.

Persistent Cookie: persistent cookie is a cookie which is stored in a cookie file permanently on the browser’s computer.

Which is live for a time period defines by programmer in the script.

Example:

<?php

$value = ‘value’;
setcookie("testcookie", $value, time()+3600); /* expire in 1 hour */

// Where 3600 count in 60*60*1 [second*minute*hour]

?>

Article written by admin

Days between two given dates using PHP?

January 10, 2010 | Filed Under PHP | Comments Off

$date1 = date(‘Y-m-d’); // yyyy-mm-dd  format
$date2 = ’2006-07-01′;
$days = (strtotime() – strtotime()) / (60 * 60 * 24);
echo “Number of days since ’2006-07-01′: $days”;

Where strtotime

int strtotime ( string $time [, int $now] )

The function expects to be given  a string containing a US English date format and will try to parse that format into a UNIX timestamp (the number of seconds since January 1 1970 00:00:00 GMT), relative to the timestamp given in now, or the current time if now is not supplied.

Article written by admin

How can we repair a MySQL table?

January 10, 2010 | Filed Under JQuery, PHP | Comments Off

There are three syntax  to repair MySQL table.

REPAIR TABLE table_name
REPAIR TABLE table_name QUICK
REPAIR TABLE table_name EXTENDED

If QUICK is given, MySQL will repair index tree.
If EXTENDED is given, it will create index row by row.

Article written by admin

what is LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN in mysql?

January 10, 2010 | Filed Under MySQL, PHP | Comments Off

The best way to learn about JOINS is the example. So below is the example to demonstrate JOINS
Suppose that there are two tables
1- Person
2-Information
Person table has definition and data like  that
Person:

Number(Primary key) name phone
1 Mr Sana 0120 234564
2 Mr Urooj 0120 234567
3 Mr Imran 0120 245678

Information:

border=”1″ width=”400″ cellspacing=”0″ cellpadding=”10″>

Entry_id Number Address
1 1 ChandPura, Bahraich UP (INDIA)
2 3 Dadi hat, Bahraich UP (INDIA)
3 3 Qazi pura, Bahraich UP (INDIA)
4 3 New Ashok Nagar, Delhi UP (INDIA)
5 4 Faizadab UP (INDIA)

mysql> select * from person;

out put:

Number(Primary key) name phone
1 Mr Sana 0120 234564
2 Mr Urooj 0120 234567
3 Mr Imran 0120 245678

3 rows in set (0.00 sec)

mysql> select * from information;

Entry_id Number Address
1 1 Chand Pura, Bahraich UP (INDIA)
2 3 Dadi hat, Bahraich UP (INDIA)
3 3 Qazi pura, Bahraich UP (INDIA)
4 3 New Ashok Nagar, Delhi UP (INDIA)
5 4 Faizadab UP (INDIA)

5 rows in set (0.00 sec)

Regular Join:

If apply a regular JOIN (with out use of these keywords INNER, OUTER, LEFT or RIGHT), then I get all records that match in the appropriate way in the two tables, and records in both incoming tables that do not match are not reported:

mysql> select name, phone, address

from person join information

on person.number = information.number;

OR

mysql> select name, phone, address

from person,information where

person.number = information.number;

Output:

name Phone Address
Mr Sana 0120 234564 Chand Pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 Dadi hat, Bahraich UP (INDIA)
Mr Imran 0120 245678 Qazi pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 New Ashok Nagar, Delhi UP (INDIA)

4 rows in set (0.01 sec)

LEFT JOIN:
If I apply left joint with using keyword LEFT JOIN

mysql> select name, phone, address
from person left join information on person.number = information.number;

Output:

name phone address
Mr Sana 0120 234564 Chand Pura, Bahraich UP (INDIA)
Mr Urooj 0120 234567 NULL
Mr Imran 0120 245678 Dadi hat, Bahraich UP (INDIA)
Mr Imran 0120 245678 Qazi pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 New Ashok Nagar, Delhi UP

5 rows in set (0.00 sec)

RIGHT JOIN:
If I apply right joint with using keyword RIGHT JOIN

mysql> select name, phone, address
from person right join information on person.number = information.number;

name phone address
Mr Sana 0120 234564 Chand Pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 Dadi hat, Bahraich UP (INDIA)
Mr Imran 0120 245678 Qazi pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 New Ashok Nagar, Delhi UP
NULL NULL Faizadab UP (INDIA)

5 rows in set (0.00 sec)

OUTER JOIN: Outer join does not support RDBMS.

Article written by admin

How to find nth highest salary

December 28, 2009 | Filed Under PHP | Comments Off

To find nth highest salary use below code.

SELECT * FROM emp employee1 WHERE n = (SELECT COUNT(DISTINCT (employee2.salary) FROM emp employee2 WHERE employee2.salary >= employee1.salary))

use find 2th highest salary put only 2 on the place of n on the above query.
the query will become

SELECT  * FROM emp employee1 WHERE 2 = (SELECT COUNT(DISTINCT (employee2.salary) FROM emp employee2 WHERE employee2.salary >= employee1.salary))

for 3,4,5 ……, put exact number on the place of n on the above query.

Article written by admin

What is the use of #id selector in JQuery library.

December 28, 2009 | Filed Under PHP | Comments Off

ID attribute play very important rule in web page.
In a web page every web element must have unique id.

Ex-<input type=”text” name=”text1” id=”text1”/>
<input type=”text” name=”text2” id=”text2”/>

To  get value of above  textbox from #id selector using jquery need to write below code.

var value=$(“# text1”).val();

another example, To apply css on #myDivId element use below code.

$(“#myDivId”).css(“border”,”3px solid red”);

Article written by admin

In how many ways we can use ajax in JQuery library

December 28, 2009 | Filed Under PHP | Comments Off

There are number of ways to use Ajax in JQuery library

GET Oriented Method:
If you want Ajax activity either on load page, submit form, paging what ever. It depend on you
Use below code


$. get (“back.php”, {name1: “php”, name2: “interview”},
function(data){
alert(“Data Loaded: ” + data);
});

Where
back.php = back file need to call.
name1: “php”, name1: “interview” = values to send on back.php.
data = value which back.php will return.

Please see look on back.php

POST Oriented Method:

Please changes $.get to $.post in above code.

Third method:

This is third method to use Ajax using JQuery library.

$.ajax({
type: “POST”,
Url: “back.php”,
data: “name=php&location=India”,
success:function (msg){
alert( “Data Saved: ” + msg);
}
});

Article written by admin

Set Timezone in php.

December 26, 2009 | Filed Under PHP | 1 Comment

Hello Visitor
Here is a example how we can set time zone in php.Time zone functionally is very important in auction sites, freelance based sites, product base sites, but many programmer don’t care about time zone.
due to this lake of programming visitor face problems and sites goes to down.
Here is an example how we can solve time zone issue according to
PHP script.

Example - Suppose you are from India and you submit a product in auction bases site, you insert end date of product according to Indian time zone but server is in UK, then automatically the end date of product will be on UK time zone( insert in mysql). but it must be show for you in Indian time zone, anybody viewing this product will show on their < country own time zone not UK based data and time.

So for this PHP Provide a solution.

1-Create a table

CREATE TABLE IF NOT EXISTS `timezone` (
`timezid` int(11) NOT NULL auto_increment,
`tz` varchar(250) NOT NULL default ”,
`gmt` text NOT NULL,
PRIMARY KEY (`timezid`)
) ENGINE=InnoDB ;

INSERT INTO `timezone` (`timezid`, `tz`, `gmt`) VALUES
(1, ‘Pacific/Kwajalein’, ‘(GMT -12:00) Eniwetok, Kwajalein’),
(2, ‘Pacific/Samoa’, ‘(GMT -11:00) Midway Island, Samoa’),
(3, ‘Pacific/Honolulu’, ‘(GMT -10:00) Hawaii’),
(4, ‘America/Anchorage’, ‘(GMT -9:00) Alaska’),
(5, ‘America/Los_Angeles’, ‘(GMT -8:00) Pacific Time (US & Canada) Los Angeles, Seattle’),
(6, ‘America/Denver’, ‘(GMT -7:00) Mountain Time (US & Canada) Denver’),
(7, ‘America/Chicago’, ‘(GMT -6:00) Central Time (US & Canada), Chicago, Mexico City’),
(8, ‘America/New_York’, ‘(GMT -5:00) Eastern Time (US & Canada), New York, Bogota, Lima’),
(9, ‘Atlantic/Bermuda’, ‘(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz’),
(10, ‘Canada/Newfoundland’, ‘(GMT -3:30) Newfoundland’),
(11, ‘Brazil/East’, ‘(GMT -3:00) Brazil, Buenos Aires, Georgetown’),
(12, ‘Atlantic/Azores’, ‘(GMT -2:00) Mid-Atlantic’),
(13, ‘Atlantic/Cape_Verde’, ‘(GMT -1:00 hour) Azores, Cape Verde Islands’),
(14, ‘Europe/London’, ‘(GMT) Western Europe Time, London, Lisbon, Casablanca’),
(15, ‘Europe/Brussels’, ‘(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris’),
(16, ‘Europe/Helsinki’, ‘(GMT +2:00) Kaliningrad, South Africa’),
(17, ‘Asia/Baghdad’, ‘(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg’),
(18, ‘Asia/Tehran’, ‘(GMT +3:30) Tehran’),
(19, ‘Asia/Baku’, ‘(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi’),
(20, ‘Asia/Kabul’, ‘(GMT +4:30) Kabul’),
(21, ‘Asia/Karachi’, ‘(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent’),
(22, ‘Asia/Calcutta’, ‘(GMT +5:30) Bombay, Calcutta, Madras, New Delhi’),
(23, ‘Asia/Dhaka’, ‘(GMT +6:00) Almaty, Dhaka, Colombo’),
(24, ‘Asia/Bangkok’, ‘(GMT +7:00) Bangkok, Hanoi, Jakarta’),
(25, ‘Asia/Hong_Kong’, ‘(GMT +8:00) Beijing, Perth, Singapore, Hong Kong’),
(26, ‘Asia/Tokyo’, ‘(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk’),
(27, ‘Australia/Adelaide’, ‘(GMT +9:30) Adelaide, Darwin’),
(28, ‘Pacific/Guam’, ‘(GMT +10:00) Eastern Australia, Guam, Vladivostok’),
(29, ‘Asia/Magadan’, ‘(GMT +11:00) Magadan, Solomon Islands, New Caledonia’),
(30, ‘Pacific/Fiji’, ‘(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka’);

This table contain all countries timezone parameter.

2-PHP function

function getCorrectTime($date_time_string, $tz, $date_format = “r”) {

$time = strtotime($date_time_string);

$tz_bak = getenv(“TZ”);

putenv(“TZ=$tz”);

$validdate= date($date_time_string, $time);

putenv(“TZ” . ($tz_bak ? “=$tz_bak” : “”));

return $validdate;

}

where
$date_time_string is inserted date/datetime in database.

$tz is the time zone value from database like Asia/Calcutta for INDIA

$date_format is date/datetime format in which format you want.

Usage

$date=’2009-06-29 11:02′ // value from data base

$timezone=’Asia/Calcutta’; // you want indian time zone

$format=’F d,Y H:i a’ // give format of date and time in which format you want date and time
getCorrectTime($date,$timezone,$format);

Article written by admin

File reading/downloading using php.

December 23, 2009 | Filed Under PHP | Comments Off

This is the example, how to download pdf file using php. you can also use this code for zip file,word file,image file, any type of media file.

$filename=”directory/filename”;//In $filename will have to  provide full path of file.

header(“Pragma: public”);

header(“Expires: 0″);

header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″);

header(“Content-Type: application/force-download”);

header(“Content-Type: application/octet-stream”);

header(“Content-Type: application/download”);

header(“Content-Disposition: attachment; filename=”.basename($filename).”;”);

header(“Content-Transfer-Encoding: binary”);

header(“Content-type: application/pdf”);

header(“Content-Length: “.filesize($filename));

readfile(“$filename”);

exit();

If your file is pdf then use header(“Content-type: application/pdf”);

If zip file then use  header(“Content-type: application/zip”);

In $filename provide full path of file.

Ex. If your file is in  pdf folder and your php file is in  outside of pdf folder.

Then $filename=”pdf/filename.pdf”;//  will be.

If you want to display file not download, You have to remove below header  from above code.

header(“Content-Disposition: attachment; filename=”.basename($filename).”;”);


Article written by admin

Difference between include_once() and require_once().

December 19, 2009 | Filed Under PHP | Comments Off

Both are  used to includes files once. Means PHP will check if the file has already been included, and if so, not include (include) it again.

include _once() create  warning  if file not found and  execute   script.

require_once() create fatal  error  if file not found and  terminate  script.

Article written by admin

© PHPInterviewQuestion.com 2009 - 2012

eXTReMe Tracker