0% found this document useful (0 votes)
81 views10 pages

How Do You Define A Constant?

The document discusses various PHP concepts including: 1. Defining constants using define(), and the differences between require, include, include_once - include_once and require_once only include a file once, while require and include may include multiple times. 2. Encoding and decoding URLs using urlencode() and urldecode(). urlencode converts special characters for safe URLs, while urldecode reverses the encoding. 3. Getting uploaded file information in PHP using the $_FILES superglobal array, which contains data like the file name, type, size and temp filename. 4. The difference between mysql_fetch_object and mysql_fetch_array - fetch_object returns a single record, while fetch_

Uploaded by

Milind Kolte
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views10 pages

How Do You Define A Constant?

The document discusses various PHP concepts including: 1. Defining constants using define(), and the differences between require, include, include_once - include_once and require_once only include a file once, while require and include may include multiple times. 2. Encoding and decoding URLs using urlencode() and urldecode(). urlencode converts special characters for safe URLs, while urldecode reverses the encoding. 3. Getting uploaded file information in PHP using the $_FILES superglobal array, which contains data like the file name, type, size and temp filename. 4. The difference between mysql_fetch_object and mysql_fetch_array - fetch_object returns a single record, while fetch_

Uploaded by

Milind Kolte
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

How do you define a constant?

Via define() directive, like define (MYCONSTANT, 100); What are the differences between require and include, include_once? Anwser 1: require_once() and include_once() are both the functions to include and evaluate the specified file only once. If the specified file is included previous to the present call occurrence, it will not be done again. But require() and include() will do it as many times they are asked to do. Anwser 2: The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. The major difference between include() and require() is that in failure include() produces a warning message whereas require() produces a fatal errors. Anwser 3: All three are used to an include file into the current page. If the file is not present, require(), calls a fatal error, while in include() does not. The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. It des not call a fatal error if file not exists. require_once() does the same as include_once(), but it calls a fatal error if file not exists. Anwser 4: File will not be included more than once. If we want to include a file once only and further calling of the file will be ignored then we have to use the PHP function include_once(). This will prevent problems with function redefinitions, variable value reassignments, etc. What is meant by urlencode and urldecode? Anwser 1: urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode(10.00%) will return 10%2E00%25. URL encoded strings are safe to be used as part of URLs. urldecode() returns the URL decoded version of the given string. Anwser 2: string urlencode(str) Returns the URL encoded version of the input string. String values to be used in URL query string need to be URL encoded. In the URL encoded version: Alphanumeric characters are maintained as is. Space characters are converted to + characters.

Other non-alphanumeric characters are converted % followed by two hex digits representing the converted character. string urldecode(str) Returns the original string of the input URL encoded string. For example: $discount =10.00%; $url = http://domain.com/submit.php?disc=.urlencode($discount); echo $url; You will get http://domain.com/submit.php?disc=10%2E00%25. How To Get the Uploaded File Information in the Receiving Script? Once the Web server received the uploaded file, it will call the PHP script specified in the form action attribute to process them. This receiving PHP script can get the uploaded file information through the predefined array called $_FILES. Uploaded file information is organized in $_FILES as a two-dimensional array as: $_FILES[$fieldName]['name'] The Original file name on the browser system. $_FILES[$fieldName]['type'] The file type determined by the browser. $_FILES[$fieldName]['size'] The Number of bytes of the file content. $_FILES[$fieldName]['tmp_name'] The temporary filename of the file in which the uploaded file was stored on the server. $_FILES[$fieldName]['error'] The error code associated with this file upload. The $fieldName is the name used in the <INPUT,>. What is the difference between mysql_fetch_object and mysql_fetch_array? MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array How can I execute a PHP script using command line? 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. I am trying to assign a variable the value of 0123, but it keeps coming up with a different number, whats the problem? PHP Interpreter treats numbers beginning with 0 as octal. Look at the similar PHP interview questions for more numeric problems.

How many ways can we get the value of current session id? session_id() returns the session id for the current session. What is the difference between mysql_fetch_object and mysql_fetch_array? MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array What is meant by urlencode and urldecode? Anwser 1: urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode(10.00%) will return 10%2E00%25. URL encoded strings are safe to be used as part of URLs. urldecode() returns the URL decoded version of the given string. Anwser 2: string urlencode(str) Returns the URL encoded version of the input string. String values to be used in URL query string need to be URL encoded. In the URL encoded version: Alphanumeric characters are maintained as is. Space characters are converted to + characters. Other non-alphanumeric characters are converted % followed by two hex digits representing the converted character. string urldecode(str) Returns the original string of the input URL encoded string. For example: $discount =10.00%; $url = http://domain.com/submit.php?disc=.urlencode($discount); echo $url; You will get http://domain.com/submit.php?disc=10%2E00%25. What is the difference between $message and $$message? Anwser 1: $message is a simple variable whereas $$message is a reference variable. Example: $user = bob is equivalent to

$holder = user; $$holder = bob; Anwser 2: They are both variables. But $message is a variable with a fixed name. $$message is a variable whos name is stored in $message. For example, if $message contains var, $$message is the same as $var. What does a special set of tags do in PHP? What does a special set of tags <?= and ?> do in PHP? The output is displayed directly to the browser. What Is a Persistent Cookie? A persistent cookie is a cookie which is stored in a cookie file permanently on the browsers computer. By default, cookies are created as temporary cookies which stored only in the browsers memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:

Temporary cookies can not be used for tracking long-term information. Persistent cookies can be used for tracking long-term information. Temporary cookies are safer because no programs other than the browser can access them. Persistent cookies are less secure because users can open cookie files see the cookie values

How can we encrypt the username and password using PHP? Answer1 You can encrypt a password with the following Mysql>SET PASSWORD=PASSWORD(Password); Answer2 You can use the MySQL PASSWORD() function to encrypt username and password. For example, INSERT into user (password, ) VALUES (PASSWORD($password)), ); How do I find out the number of parameters passed into function. ? func_num_args() function returns the number of parameters passed in.

Various PHP Programming Tricks


Couple of nice PHP Programming Tricks. You can take a look at the various tricks in these sub pages. Force a secure HTTP connection if (!($HTTPS == on)) { header (Location: https://$SERVER_NAME$php_SELF); exit; } Get the date $today = getdate(); $month = $today['month']; $mday = $today['mday']; $year = $today['year']; Random Loading You can load random stuff by using this code. For this example, I load random color code: $selectnumber = rand (1, 5); if($selectnumber==1) $pagebg=#990000; if($selectnumber==2) $pagebg=#0000FF; if($selectnumber==3) $pagebg=#00AAAA; if($selectnumber==4) $pagebg=#000099; if($selectnumber==5) $pagebg=#DDDD00; Easy Way to List Directory Structure $path = /home/user/public/foldername/; $dir_handle = @opendir($path) or die(Unable to open $path); while ($file = readdir($dir_handle)) { if($file == . || $file == .. || $file == index.php ) continue; echo <a href=\$file\>$file</a><br />; } closedir($dir_handle); Easy Way to Optimize Database Table dbConnect() $alltables = mysql_query(SHOW TABLES); while ($table = mysql_fetch_assoc($alltables)) {

foreach ($table as $db => $tablename) { mysql_query(OPTIMIZE TABLE .$tablename.) or die(mysql_error()); } } Create a password protect webpage <? $username = someuser; $password = somepassword; if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {?> <h1>Login</h1> <form name=form method=post action=<?php echo $_SERVER['PHP_SELF']; ?>> <p><label for=txtUsername>Username:</label> <br><input type=text title=Enter your Username name=txtUsername></p> <p><label for=txtpassword>Password:</label> <br><input type=password title=Enter your password name=txtPassword></p> <p><input type=submit name=Submit value=Login></p> </form> <?} else {?> <p>This is the protected page. Your private content goes here.</p> <?}?> Reference: http://www.osdw.org/Various_PHP_Tricks.html

What is htaccess ? htaccess stand for hypertext access.It is the default name of a directory-level configuration file that allows for decentralized management of web server configuration. Common usage are Authorization, Authentication, URLs Rewriting ,Blocking ,Customized error responses,MIME types , Cache Control and Directory listing ,etc Whats PHP ? The PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications. What Is a Session? A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests. There is only one session object available to your PHP scripts at any time. Data saved to the session by a script can be retrieved by the same script or another script when requested from the same visitor. Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor. What is meant by PEAR in php? PEAR is the next revolution in PHP. This repository is bringing higher level programming to PHP. PEAR is a framework and distribution system for reusable PHP components. It eases installation by bringing an automated wizard, and packing the strength and experience of PHP users into a nicely organised OOP library. PEAR also provides a command-line interface that can be used to automatically install packages How can we repair a MySQL table? The syntex for repairing a mysql table is:

REPAIR TABLE tablename REPAIR TABLE tablename QUICK REPAIR TABLE tablename EXTENDED This command will repair the table specified. If QUICK is given, MySQL will do a repair of only the index tree. If EXTENDED is given, it will create index row by row. What is the difference between $message and $message? $message is a simple variable whereas $message is a reference variable. Example: $user = me is equivalent to $holder = user; $holder = me; What Is a Persistent Cookie? A persistent cookie is a cookie which is stored in a cookie file permanently on the browsers computer. By default, cookies are created as temporary cookies which stored only in the browsers memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences: *Temporary cookies cannot be used for tracking long-term information. *Persistent cookies can be used for tracking long-term information. *Temporary cookies are safer because no programs other than the browser can access them. *Persistent cookies are less secure because users can open cookie files see the cookie values.

What is the difference between ereg_replace() and eregi_replace()? eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters. How do I find out the number of parameters passed into function9. ? func_num_args() function returns the number of parameters passed in. What is the purpose of the following files having extensions: frm, myd, and myi? What these files contain? In MySQL, the default table type is MyISAM. Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type. The .frm file stores the table definition. The data file has a .MYD (MYData) extension. The index file has a .MYI (MYIndex) extension, If the variable $a is equal to 5 and variable $b is equal to character a, whats the value of $ $b? 100, its a reference to existing variable. How To Protect Special Characters in Query String? If you want to include special characters like spaces in the query string, you need to protect them by applying the urlencode() translation function. The script below shows how to use urlencode(): <?php print(<html>); print(<p>Please click the links below . to submit comments about TECHPreparation.com:</p>); $comment = I want to say: It\s a good site! :->; $comment = urlencode($comment); print(<p> .<a href=\processing_forms.php?name=Guest&comment=$comment\> .Its an excellent site!</a></p>); $comment = This visitor said: It\s an average site! ; $comment = urlencode($comment); print(<p> .<a href=processing_forms.php?.$comment.> .Its an average site.</a></p>); print(</html>); ?> Are objects passed by value or by reference? Everything is passed by value.

What are the differences between DROP a table and TRUNCATE a table? DROP TABLE table_name This will delete the table and its data. TRUNCATE TABLE table_name This will delete the data of the table, but not the table definition. How do you call a constructor for a parent class? parent::constructor($value) WHAT ARE THE DIFFERENT TYPES OF ERRORS IN PHP? Here are three basic types of runtime errors in PHP: 1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all although you can change this default behavior. 2. Warnings: These are more serious errors for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination. 3. Fatal errors: These are critical errors for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHPs default behavior is to display them to the user when they take place. Internally, these variations are represented by twelve different error types Whats the special meaning of __sleep and __wakeup? __sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them. How can we submit a form without a submit button? If you dont want to use the Submit button to submit a form, you can use normal hyper links to submit a form. But you need to use some JavaScript code in the URL of the link. For example: <a href=javascript: document.myform.submit();>Submit Me</a>

You might also like