PHP Programming Basics

These are the elements of PHP that you will deal with on a constant basis, since they are at the very foundation of the language itself.

20 cards   |   Total Attempts: 183
  

Related Topics

Cards In This Set

Front Back
Choose the selection that best matches the following statements: PHP is a _____ scripting language based on the ____ engine. It is primarily used to develop dynamic _____ content, although it can be used to generate ____ documents (among others) as well. A. Dynamic, PHP, Database, HTML B. Embedded, Zend, HTML, XML C. Perl-based, PHP, Web, Static D. Embedded, Zend, Docbook, MySQL E. Zend-based, PHP, Image, HTML
Looking at the answers, the only one that makes sense for every blank is B. PHP is a scripting language based on the Zend Engine that is usually embedded in HTML code. As such, it is primarily used to develop HTML documents, although it can be used just as nicely to develop other types of documents, such as XML.
Which of the following tags is not a valid way to begin and end a PHP code block? A. <% %> B. <? ?> C. <?= ?> D. <! !> E. <?php ?>
While tags such as and are often forgotten in PHP programming, they are valid ways to delimit a PHP code block. The tags, however, are not valid and, therefore, the correct answer is D. Keep in mind, in any case, that some of these tags are not always available, depending on how the php.ini file on which the PHP interpreter runs is configured.
Which of the following is not valid PHP code? A. $_10 B. ${“MyVar”} C. &$something D. $10_somethings E. $aVaR
PHP variables always start with a dollar sign and are a sequence of characters and numbers within the Latin alphabet, plus the underscore character. ${"MyVar"} is a valid variable name that simply uses a slightly less common naming convention, while &$something is a reference to the $something variable. Variables, however cannot start with numbers, making $10_somethings invalid and Answer D correct.
What is displayed when the following script is executed?
The important thing to note here is that the $myarray array’s key value is being referenced without quotes around it. Because of this, the key being accessed is not the myvalue string but the value represented by the myvalue constant. Hence, it is equivalent to accessing $myarray[10], which is Dog, and Answer A is correct.
What is the difference between print() and echo()?A. print() can be used as part of an expression, whule echo() can't B. echo() can be used as part of an expression, while print() can't C. echo() can be used in the CLI version of PHP, while print() can't D. print() can be used in the CLI version of PHP, while echo() can't E. There's no difference: both functons print out some text!
Even though print() and echo() are essentially interchangeable most of the time, there is a substantial difference between them. While print() behaves like a function with its own return value (although it is a language construct), echo() is actually a language construct that has no return value and cannot, therefore, be used in an expression. Thus, Answer A is correct.
What is the output of the following script?
Other than the simple math, the % operator is a modulus, which returns whatever the remainder would be if its two operands were divided. The << operator is a left-shift operator, which effectively multiplies an integer number by powers of two. Finally, the ultimate answer is multiplied by a floating point and, therefore, its type changes accordingly. However, the result is still printed out without any fractional part, since the latter is nil. The final output is 256 (Answer D).
Which values should be assigned to the variables $a, $b and $c in order for the following script to display the string Hello, World!?
Following the logic of the conditions, the only way to get to the Hello, World! string is in the else condition of the first if statement. Thus, $a must be False. Likewise, $b must be False. The final conditional relies on both previous conditions ($a and $b) being False, but insists that $c be True (Answer D).
What will the following script output?
The correct answer is C. As of PHP 4.2.0, there is no need to initialize the random number generator using srand() unless a specific sequence of pseudorandom numbers is sought. Besides, even if the random number generator had not been seeded, the script would have still outputted 49 pseudo-random characters—the same ones every time. The $array variable, though a string, can be accessed as an array, in which case the individual characters corresponding to the numeric index used will be returned. Finally, the for loop starts from 1 and continues until $i is less than 50—for a total of 49 times.
Which language construct can best represent the following series of if conditionals?
A series of if…else if code blocks checking for a single condition as above is a perfect place to use a switch statement:
What is the best way to iterate through the $myarray array, assuming you want to modify the value of each element as you do?
Normally, the foreach statement is the most appropriate construct for iterating through an array. However, because we are being asked to modify each element in the array, this option is not available, since foreach works on a copy of the array and would therefore result in added overhead. Although a while loop or a do…while loop might work, because the array is sequentially indexed a for statement is best suited for the task, making Answer A correct:
Consider the following segment of code:
As it is only possible to add a single line of code to the segment provided, the only statement that makes sense is a for loop, making the choice either C or D. In order to select the for loop that actually produces the correct result, we must first of all revisit its structural elements. In PHP, for loops are declared as follows: for (; ; ) where the is executed prior to entering the loop. The for loop then begins executing the code within its code block until the statement evaluates to False. Every time an iteration of the loop is completed, the is executed. Applying this to our code segment, the correct for statement is: for ($idx = 1; $idx < STOP_AT; $idx *= 2) for answer C.
Choose the appropriate function declaration for the user-defined function is_leap(). Assume that, if not otherwise defined, the is_leap function uses the year 2000 as a default value:
Of the five options, only two are valid PHP function declarations (A and D). Of these two declarations, only one will provide a default parameter if none is passed—Answer A.
What is the value displayed when the following is executed? Assume that the code was executed using the following URL: testscript.php?c=25
This question is designed to test your knowledge of how PHP scopes variables when dealing with functions. Specifically, you must understand how the global statement works to bring global variables into the local scope, and the scope-less nature of superglobal arrays such as $_GET, $_POST, $_COOKIE, $_REQUEST and others. In this case, the math works out to 5 + 25 - 25 – 10, which is -5, or answer B.
Consider the following script:
Functions can be called dynamically by appending parentheses (as well as any parameter needed) to a variable containing the name of the function to call. Thus, for Group A the appropriate index combination is 0, 4, 9, 9, 9, 9, which evaluates to the string myfunction. The parameters, on the other hand, are evaluated as variables dynamically using the ${} construct. This means the appropriate indexes for group B are 7 and 8, which evaluate to ${'a'} and ${'b'}—meaning the variables $a and $b respectively. Therefore, the correct answer is D.
Run-time inclusion of a PHP script is performed using the ________ construct, while compile-time inclusion of PHP scripts is performed using the _______ construct. A. include_once, include B. require, include C. require_once, include D. include, require E. All of the above are correct
In recent versions of PHP, the only difference between require() (or require_once()) and include() (or include_once()) is in the fact that, while the former will only throw a warning and allow the script to continue its execution if the include file is not found, the latter will throw an error and halt the script. Therefore, Answer E is correct.