Friday, January 17, 2014

PHP Test for Beginners

If you're currently learning PHP (the programming/scripting language) then keep reading, we've put together a little beginners quiz just for you. Some of these questions are more about trivia and tips rather than actual language questions, so if you're not a beginner, consider reading along anyway, we'll try to keep it interesting.

1. What is a simple way to check your PHP version and configuration?

2. What is a way to get the IP address of the server?

3. What is a simple way to get the first character in the string variable $strx?

4. The file fxfunc.php is an include file and contains no HTML. How many tags should be present after the opening PHP tag?

5. How can I make an if statement without using a closing brace (})?

6. What is the difference between null and NULL?

7. When making an class, how do you make a member function the constructor so it is called on creation?

8. What is a simple way to print a string as text rather than HTML when it may contain tags?

9. Imagine you have found a bug in your program. You are printing values from an array, using something similar to implode, but they print in the wrong order, such as printing key 0, then 2, then 1. How do you fix it with minimal code?

10. register_globals doesn't exist in new versions of PHP, why?

Answer: Create a PHP file containing PHP tags with the phpinfo() function inside. You'll want to view the file in a browser since it will usually show a properly formatted HTML page.

Example of wrong/poor answer: Fetch the version with phpversion() and configuration options with ini_get(). (Not Simple)

Answer: In almost all cases it is in the $_SERVER['SERVER_ADDR'] variable.

Example of wrong/poor answer: $_SERVER['REMOTE_ADDR'] (This would be the client's IP and not usually the server's)

Answer: There are multiple acceptable answers. $strx[0] or $strx{0} are the same so both are good. Slightly more complex is substr($strx, 0, 1) or other string functions.

Example of wrong/poor answer: $strx[1] (Second character)

Answer: Presumably no tags should be there past the opening PHP tag. As recommended in the manual, you can and should omit the closing PHP tag to make sure no whitespace shows up. It's not required though.

Example of wrong/poor answer: (Depends on if you agree with the answer above.)

Answer: There are multiple answers, one is that as long as the if statement has one nested statement then it doesn't need opening and closing braces. If you do have multiple statements you can use if(): and endif; rather than the more common if(){ and }.

Example of wrong/poor answer: (Multiple lines without any closing syntax would be wrong.)

Answer: Technically null is a variable type and NULL is a value and is the only value for null types. That's what the manual describes, but probably won't matter when you are writing code, just consider it trivia, and also remember $var = NULL; seems to be the official way to assign it.

Example of wrong/poor answer: null is never used (It is invalid or non-traditional in some languages)

Answer: Name the constructor member function __construct().

Example of wrong/poor answer: Name  it construct(), init() or the class's name. (It's possible to get other language's syntax confused with PHP's)

There are probably a lot of ways, but the answer we were looking for is the htmlentities() function. It makes sure that text appears as text and is not interpreted as tags or hidden characters.

Example of wrong/poor answer: Enclosing it in an element (such as pre or textarea) in an HTML document. (This could result in breaking the element tags or causing other issues.)

Answer: Use the ksort() function on the array before it is printed, possibly with the SORT_NUMERIC flag.

Example of wrong/poor answer: (Sorting by looping and assigning a new array by numbers probably isn't a good choice)

Answer: This is another bit of trivia. register_globals was made off by default in 2002 and it has generally (often emphatically) been suggested that it remain off. The reason that it was considered a problem was that it caused variables, GET and POST variables, to appear as normal variables. It had a tendency to increase bugs and vulnerabilities. Perhaps more importantly it encouraged the writing of confusing code with variables being used that may or may not have been defined, and that always needed register_globals. It was removed in 2012 along with safe_mode (source: PHP entry in Wikipedia).

1 comment:

  1. Very good topic. Most people have issues with this.