PHP Important Questions
What is PHP?
PHP is an open source server side scripting language commonly used for web applications. What is Open Source Software?
Software in which the source codes are freely used, modify, and shared by anyone are called Open Source Software. These can also be distributed under licenses that adhere with the Open Source Definition. What are the different errors in PHP?
There are 4 basically types of error. Parse Error – Commonly caused due to syntax mistakes in codes e.g. missing semicolon, mismatch brackets. Fatal Error – These are basically run time errors which are caused when you try to access what can’t be done. E.g. accessing a dead obect, or trying to use a function that hasn’t been declared. Warning Error – These occurs when u try to include a file that is not present, or delete a file that is not on the ser!er. This will not halt the script" it will gi!e the notice and continue with the next line of the script. Notice Error – These errors occurs when u try to use a !ariable that hasn’t been declared, this will not halt the script, #t will gi!e the notice and continue with the next line of the script. What is session and why do we use it?
$ession is a super global !ariable that preser!e data across subse%uent pages. $ession uni%uely defines each user with a session #&, so it helps making customi'ed web application where user tracking is needed. What is cookie and why do we use it?
Cookie is a small piece of information stored in client browser. #t is a techni%ue used to identify a user using the information stored in their browser (if already !isited that website) . *sing ++ we can both set and get C--#E. How to print current date and time.
<php echo date!"#$m$d H%i%s"&'
( What function do we use to find length of string, and length of array?
/or finding length of string we use strlen( function and for array we use count( function. How can we change the !alue of a constant?
0e cannot change the !alue of a constant. What is the difference "etween unset( and unlink( function. unset( is used to destroy a !ariable where as unlink( is used to destroy a file. How do we get the current session #$?
<php session)start!&' echo session)id!&' ( How do we destroy a session.
12php session3start()" session3destroy()" 2 What is the difference "etween e%plode( and split( functions? 5oth are used to split a string to array, the basic difference is that split( uses pattern for splitting and e%plode( uses a string. explode() is faster than split() as it does not match the string based on regular expression. 6lso split() is deprecated as of 7.8.9. $o using of this function is discouraged. What is an associati!e array?
6ssociati!e arrays are arrays that use named keys that you assign to them. 12php :capitals;array(<#ndia<;<=ew &elhi<,access abstraction layer, so no matter what database
we use the function to issue %ueries and fetch data will be same. *sing +&- dri!ers we can connect to database like &5?, -racle, +ostgre$@A etc. What is the difference "etween 'a!ascript and PHP?
Ba!ascript is a client side scripting language whereas ++ is a ser!er side scripting language. What is ))?
C$$ or cascading $tyle $heet is a way to style and present TA. What is the difference "etween #$ and class in ))?
The difference between an #& and Class is that an #& can be used to identify one element, whereas a class can be used to identify more than one. How can we su"mit a form without using su"mit "uttons?
0e can use a!ascript submit function. 0e can either use form name or form id to print
document.get*lement+yd!-formD-&.submit!&' document.formname.submit!&' What is the difference "etween s*l and +ys*l?
$@A or $tructured @uery Aanguage is a programming language designed for managing data held in a Delational &atabase anagement $ystem. ys%l is a open source, relational database management $ystem. Why do we use -&P /0 and &-$E- /0 function in mys*l?
roup 5y is used for retrie!ing information about a group of data. #t is generally used with some aggregate function like $*, 6F etc. -D&ED 5G is used to sort the records using column name. #t can sort column in both ascending and descending order.
1 What is PHP?
++ is a web language based on scripts that allows de!elopers to dynamically create generated web pages. 2 What does the initials of PHP stand for?
++ means ++H ypertext +reprocessor. 3 Which programming language does PHP resem"le to?
++ syntax resembles +erl and C 4 What does PE5- stands for?
+E6D means I++ Extension and 6pplication DepositoryJ. it extends ++ and pro!ides a higher le!el of programming for web de!elopers. 6 What is the actually used PHP !ersion?
Fersion 7 is the actually used !ersion of ++. 7 How do you e%ecute a PHP script from the command line?
Bust use the ++ command line interface (CA#) and specify the file name of the script to be executed as followsH php scr ipt.php
K
php script.php
8 How to run the interacti!e PHP shell from the command line interface?
Bust use the ++ CA# program with the option >a as followsH php $a
K
php >a
9 What are the correct and the most two common way to start and finish a PHP "lock of code?
The two most common ways to start and finish a ++ script areH 12php L M ++ codeM> N 2 and 12 LM ++ code MN 2 : How can we display the output directly to the "rowser?
To be able to display the output directly to the browser, we ha!e to use the special tags 12; and 2. 1; What is the main difference "etween PHP 4 and PHP 6?
++ 7 presents many additional --+ (-bect -riented +rogramming) features. 11 #s multiple inheritance supported in PHP?
++ includes only single inheritance, it means that a class can be extended from only one single class using the keyword Oextended’. 12 What is the meaning of a final class and a final method?
Ofinal’ is introduced in ++7. /inal class means that this class cannot be extended and a final method cannot be o!errided. 13 How comparison of o"'ects is done in PHP6?
0e use the operator O;;’ to test is two obect are instanced from the same class and ha!e same attributes and e%ual !alues. 0e can test if two obect are refering to the same instance of the same class by the use of the identity operator O;;;’. 14 How can PHP and H<+= interact?
#t is possible to generate TA through ++ scripts, and it is possible to pass informations from TA to ++. 16 What type of operation is needed when passing !alues through a form or an -=?
#f we would like to pass !alues througn a form or an *DA then we need to encode and to decode them using htmlspecialchars() and urlencode(). 17 How can PHP and >a!ascript interact?
++ and Ba!ascript cannot directly interacts since ++ is a ser!er side language and Ba!ascript is a client side language. owe!er we can exchange !ariables since ++ is able to generate Ba!ascript code to be executed by the browser and it is possible to pass specific !ariables back to ++ !ia the *DA.
18 What is needed to "e a"le to use image function?
& library is needed to be able execute image functions. 19 What is the use of the function imagetypes(@?
imagetypes() gi!es the image format and types supported by the current !ersion of &>++. 1: What are the functions to "e used to get the image@s properties (siAe, width and height?
The functions are getimagesi'e() for si'e, imagesx() for width and imagesy() for height. 2; How failures in e%ecution are handled with include( and re*uire( functions?
#f the function re%uire() cannot access to the file then it ends with a fatal error. owe!er, the include() function gi!es a warning and the ++ script continues to execute. 21 What is the main difference "etween re*uire( and re*uireBonce(?
re%uire() and re%uire3once() perform the same task except that the second function checks if the ++ script is already included or not before executing it. (same for include3once() and include()) 22 How can # display te%t with a PHP script?
Two methods are possibleH <$$php echo -/ethod 0-' print
K
1P>>2php echo <ethod K<" print <ethod ?<" 2>>
23 How can we display information of a !aria"le and reada"le "y human with PHP?
To be able to display a human>readable result we use print3r(). 24 How is it possi"le to set an infinite e%ecution time for PHP script?
The set3time3limit(9) added at the beginning of a script sets to infinite the time of execution to not ha!e the ++ error Omaximum execution time exceeded’.#t is also possible to specify this in the php.ini file.
26 What does the PHP error Parse error in PHP C une%pected
This is a ++ syntax error expressing that a mistake at the line x stops parsing and executing the program. 27 What should we do to "e a"le to e%port data into an E%cel file?
The most common and used way is to get data into a format supported by Excel. /or example, it is possible to write a .cs! file, to choose for example comma as separator between fields and then to open the file with Excel. 28 What is the function fileBgetBcontents( usefull for?
file3get3contents() lets reading a file and storing it in a string !ariable. 29 How can we connect to a +y)D= data"ase from a PHP script?
To be able to connect to a y$@A database, we must use mys%l3connect() function as followsH <$$php 1database 2 mys3l)co
K
1P>>2php :database ; mys%l3connect(<-$T<, <*$ED3=6E<, <+6$$0-D&<)" mys%l3select3db(<&6T656$E3=6E<,:database)" 2>>
2: What is the function mys*lBpconnect( usefull for?
mys%l3pconnect() ensure a persistent connection to the database, it means that the connection do not close when the the ++ script ends. 3; How the result set of +ys*l "e handled in PHP?
The result set can be handled using mys%l3fetch3array, mys%l3fetch3assoc, mys%l3fetch3obect or mys%l3fetch3row. 31 How is it possi"le to know the num"er of rows returned in result set?
The function mys%l3num3rows() returns the number of rows in a result set. 32 Which function gi!es us the num"er of affected entries "y a *uery?
mys%l3affected3rows() return the number of entries affected by an $@A %uery. 33 What is the difference "etween mys*lBfetchBo"'ect( and mys*lBfetchBarray(?
The mys%l3fetch3obect() function collects the first single matching record where mys%l3fetch3array() collects all matching records from the table in an array. 34 How can we access the data sent through the -= with the E< method?
#n order to access the data sent !ia the ET method, we you use :3ET array like thisH www.url.com2!ar;!alue :!ariable ; :3ETLI!arJN" this will now contain O!alue’ 36 How can we access the data sent through the -= with the P&)< method?
To access the data sent this way, you use the :3+-$T array. #magine you ha!e a form field called O!ar’ on the form, when the user clicks submit to the post form, you can then access the !alue like thisH :3+-$TLI!arJN" 37 How can we check the !alue of a gi!en !aria"le is a num"er?
#t is possible to use the dedicated function, is3numeric() to check whether it is a number or not. 38 How can we check the !alue of a gi!en !aria"le is alphanumeric?
#t is possible to use the dedicated function, ctype3alnum to check whether it is an alphanumeric !alue or not. 39 How do # check if a gi!en !aria"le is empty?
#f we want to check whether a !ariable has a !alue or not, it is possible to use the empty() function. 3: What does the unlink( function means?
The unlink() function is dedicated for file system handling. #t simply deletes the file gi!en as entry. 4; What does the unset( function means?
The unset() function is dedicated for !ariable management. #t will make a !ariable undefined. 41 How do # escape data "efore storing it into the data"ase?
addslashes function enables us to escape data before storage into the database. 42 How is it possi"le to remo!e escape characters from a string?
The stripslashes function enables us to remo!e the escape characters before apostrophes in a string. 43 How can we automatically escape incoming data?
0e ha!e to enable the agic %uotes entry in the configuration file of ++. 44 What does the function getBmagicB*uotesBgpc( means?
The function get3magic3%uotes3gpc() tells us whether the magic %uotes is switched on or no. 46 #s it possi"le to remo!e the H<+= tags from data?
The strip3tags() function enables us to clean a string from the TA tags. 47 what is the static !aria"le in function useful for?
6 static !ariable is defined within a function only the first time and its !alue can be modified during function calls as followsH <$$php function test4unction!&
1P>>2php function test/unction() Q static :testFariable ; K" echo :testFariable" K :testFariableRR" S test/unction()" K test/unction()" ? test/unction()"
8 2>>
48 How can we define a !aria"le accessi"le in functions of a PHP script?
This feature is possible using the global keyword. 49 How is it possi"le to return a !alue from a function?
6 function returns a !alue using the instruction Oreturn :!alue"’. 4: What is the most con!enient hashing method to "e used to hash passwords?
#t is preferable to use crypt() which nati!ely supports se!eral hashing algorithms or the function hash() which supports more !ariants than crypt() rather than using the common hashing algorithms such as md7, shaK or sha?7U because they are concei!ed to be fast. hence, hashing passwords with these algorithms can !ulnerability. 6; Which cryptographic e%tension pro!ide generation and !erification of digital signatures?
The ++>openssl extension pro!ides se!eral cryptographic operations including generation and !erification of digital signatures. 61 How a constant is defined in a PHP script?
The define() directi!e lets us defining a constant as followsH define (I6C-=$T6=TJ, K?8)" 62 How can you pass a !aria"le "y reference?
To be able to pass a !ariable by reference, we use an ampersand in front of it, as follows :!arK ; V:!ar? 63 Will a comparison of an integer 12 and a string 13 work in PHP?
IK8J and K? can be compared in ++ since it casts e!erything to the integer type. 64 How is it possi"le to cast types in PHP?
The name of the output type ha!e to be specified in parentheses before the !ariable which is to be cast as followsH
W (int), (integer) – cast to integer W (bool), (boolean) – cast to boolean W (float), (double), (real) – cast to float W (string) – cast to string W (array) – cast to array W (obect) – cast to obect 66 When a conditional statement is ended with an endif?
0hen the original if was followed by H and then the code block without braces. 67 How is the ternary conditional operator used in PHP?
#t is composed of three expressionsH a condition, and two operands describing what instruction should be performed when the specified condition is true or false as followsH Expression3K 2 Expression3? H Expression38" 68 What is the function funcBnumBargs( used for?
The function func3num3args() is used to gi!e the number of parameters passed into a function. 69 #f the !aria"le G!ar1 is set to 1; and the G!ar2 is set to the character !ar1, what@s the !alue of GG!ar2?
::!ar? contains the !alue K9. 6: What does accessing a class !ia means?
HH is used to access static methods that do not re%uire obect initiali'ation. 7; #n PHP, o"'ects are they passed "y !alue or "y reference?
#n ++, obects passed by !alue. 71 5re Parent constructors called implicitly inside a class constructor?
=o, a parent constructor ha!e to be called explicitly as followsH parentHHconstructor(:!alue) 72 What@s the difference "etween BBsleep and BBwakeup?
33sleep returns the array of all the !ariables that need to be sa!ed, while 33wakeup retrie!es them. 73 What is faster?
K> Combining two !ariables as followsH :!ariableK ; Oello O" :!ariable? ; O0orld’" :!ariable8 ; :!ariableK.:!ariable?" -r ?> :!ariable8 ; I:!ariableK:!ariable?J" :!ariable8 will contain Iello 0orldJ. The first code is faster than the second code especially for large large sets of data. 74 what is the definition of a session?
6 session is a logical obect enabling us to preser!e temporary data across multiple ++ pages. 76 How to initiate a session in PHP?
The use of the function session3start() lets us acti!ating a session. 77 How is it possi"le to propagate a session id?
#t is possible to propagate a session id !ia cookies or *DA parameters. 78 What is the meaning of a Persistent ookie?
6 persistent cookie is permanently stored in a cookie file on the browser’s computer. 5y default, cookies are temporary and are erased if we close the browser. 79 When sessions ends?
$essions automatically ends when the ++ script finishs executing, but can be manually ended using the session3write3close(). 7: What is the difference "etween sessionBunregister( and sessionBunset(?
The session3unregister() function unregister a global !ariable from the current session and the session3unset() function free all session !ariables. 8; What does G=&/5=) means?
:A-56A$ is associati!e array including references to all !ariables which are currently defined in the global scope of the script. 81 What does GB)E-IE- means?
:3$EDFED is an array including information created by the web ser!er such as paths, headers, and script locations. 82 What does GBF#=E) means?
:3/#AE$ is an associati!e array composed of items sent to the current script !ia the TT+ +-$T method. 83 What is the difference "etween GBF#=E)Juserfile@KJname@K and GBF#=E)Juserfile@K JtmpBname@K?
:3/#AE$LOuserfile’NLOname’N represents the original name of the file on the client machine, :3/#AE$LOuserfile’NLOtmp3name’N represents the temporary filename of the file stored on the ser!er. 84 How can we get the error when there is a pro"lem to upload a file?
:3/#AE$LOuserfile’NLOerror’N contains the error code associated with the uploaded file. 86 How can we change the ma%imum siAe of the files to "e uploaded?
0e can change the maximum si'e of files to be uploaded by changing upload3max3filesi'e in php.ini. 87 What does GBENI means?
:3E=F is an associati!e array of !ariables sent to the current ++ script !ia the en!ironment method. 88 What does GB&&L#E means?
:3C--#E is an associati!e array of !ariables sent to the current ++ script using the TT+ Cookies. 89 What does the scope of !aria"les means?
The scope of a !ariable is the context within which it is defined. /or the most part all ++ !ariables only ha!e a single scope. This single scope spans included and re%uired files as well. 8: what the difference "etween the /#
:a and :bH :a V :bH
TD*E if both :a and :b are TD*E. 5its that are set in both :a and :b are set.
9; What are the two main string operators?
The first is the concatenation operator (O.’), which returns the concatenation of its right and left arguments. The second is (O.;’), which appends the argument on the right to the argument on the left. 91 What does the array operator MMM@ means?
:a ;;; :b TD*E if :a and :b ha!e the same key!alue pairs in the same order and of the same types. 92 What is the differences "etween Ga M G" and Ga MM G"?
P; means ine%uality (TD*E if :a is not e%ual to :b) and P;; means non>identity (TD*E if :a is not identical to :b). 93 How can we determine whether a PHP !aria"le is an instantiated o"'ect of a certain class?
To be able to !erify whether a ++ !ariable is an instantiated obect of a certain class we use instanceof. 94 What is the goto statement useful for?
The goto statement can be placed to enable umping inside the ++ program. The target is pointed by a label followed by a colon, and the instruction is specified as a goto statement followed by the desired target label. 96 what is the difference "etween E%ceptionget+essage and E%ceptionget=ine ?
ExceptionHHgetessage lets us getting the Exception message and ExceptionHHgetAine lets us getting the line in which the exception occurred. 97 What does the e%pression E%ceptionBBto)tring means?
ExceptionHH33to$tring gi!es the $tring representation of the exception. 98 How is it possi"le to parse a configuration file?
The function parse3ini3file() enables us to load in the ini file specified in filename, and returns the settings in it in an associati!e array. 99 How can we determine whether a !aria"le is set?
The boolean function isset determines if a !ariable is set and is not =*AA. 9: What is the difference "etween the functions strstr( and stristr(?
The string function strstr(string all$tring, string occ) returns part of all$tring from the first occurrence of occ to the end of all$tring. This function is case>sensiti!e. stristr() is identical to strstr() except that it is case insensiti!e. :; what is the difference "etween for and foreach?
for is expressed as followsH for (exprK" expr?" expr8) statement The first expression is executed once at the beginning. #n each iteration, expr? is e!aluated. #f it is TD*E, the loop continues and the statements inside for are executed. #f it e!aluates to /6A$E, the execution of the loop ends. expr8 is tested at the end of each iteration. owe!er, foreach pro!ides an easy way to iterate o!er arrays and it is only used with arrays and obects. :1 #s it possi"le to su"mit a form with a dedicated "utton?
#t is possible to use the document.form.submit() function to submit the form. /or exampleH 1input type;button !alue;J$*5#TJ onClick;Jdocument.form.submit()J :2 What is the difference "etween eregBreplace( and eregiBreplace(?
The function eregi3replace() is identical to the function ereg3replace() except that it ignores case distinction when matching alphabetic characters. :3 #s it possi"le to protect special characters in a *uery string?
Ges, we use the urlencode() function to be able to protect special characters. :4 What are the three classes of errors that can occur in PHP?
The three basic classes of errors are notices (non>critical), warnings (serious errors) and fatal errors (critical errors). :6 What is the difference "etween characters O;34 and O%34?
X984 is octal 84 and Xx84 is hex 84. :7 How can we pass the !aria"le through the na!igation "etween the pages?
#t is possible to pass the !ariables between the ++ pages using sessions, cookies or hidden form fields. :8 #s it possi"le to e%tend the e%ecution time of a php script?
The use of the set3time3limit(int seconds) enables us to extend the execution time of a php script. The default limit is 89 seconds. :9 #s it possi"le to destroy a cookie?
Ges, it is possible by setting the cookie with a past expiration time. :: What is the default session time in php?
The default session time in php is until closing of browser 1;; #s it possi"le to use &+ component in PHP?
Ges, it’s possible to integrate (&istributed) Component -bect odel components ((&)C-) in ++ scripts which is pro!ided as a framework.