two online PHP tests

August 25, 2010

This is information for some on-line PHP tests:

Even when you’ve been developing in PHP for a few years, a valid concern is whether an exam asks questions that are tricky or obscure. If so, you can miss questions on topics that you understand very well. Here are some obscure bits of information from just such a php test.


int xml_parse ( resource $parser, string $data [, bool $is_final = false ] )
The handlers for the configured events are called as many times as necessary.
If is_final is set and TRUE, the data is the last piece of data sent in this parse.

$a = 0123; // octal number (ex: equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (ex: equivalent to 26 decimal)

func_num_args() reports exactly how many arguments were passed when the function was called
The ftp_put() function uploads from a local file to the FTP server.

array_shift() shifts the first value of the array off and returns it
array_pop() pops and returns the last value of the end of the array, shortening the array by one element.

   $files = array('images', 'Mail');
   while ( list(,$file) = each($files) )
      { #       ,$file = only use only the 2nd element, 'Mail'  
      require($file);
      }

fputs — Alias of fwrite()
examples:
fputs($o, $halfstr."\n", 1024);
fputs($fp, $binarystr, strlen($binary) );
fwrite($afile, $astring, strlen($astring) );

the (3rd) maximum length parameter is optional.

power function:
$product = pow($base, $exp)
Returns $base raised to the power of $exp

mysql_ping — Ping a server connection or reconnect if there is no connection

ibase_pconnect() – Open a persistent connection to an InterBase database
ibase_connect – Open a connection to an InterBase database — to an InterBase server.
ibase_connect ($host, $username, $password);

$numbers=”1234567890″;
$digits=split(“[2468]”,$numbers);

What does the above code place into the $digits variable?
answer: an array of 1, 3, 5, 7, 90

mysql would be valuable, ibase is obscure, and connect statements don’t belong with queries.

  $host = 'localhost:/path/to/your.gdb';
  $dbh  = ibase_connect($host, $username, $password);
  $stmt = 'SELECT * FROM tblname';
  $sth  = ibase_query($dbh, $stmt);
  while ($row = ibase_fetch_object($sth)) 
     { echo $row->email, "\n"; }

here are 2 good questions

1. Find the errors in the following code:

<?
function baz($y $z) {
    $x = new Array();
    $x[sales]  = 60;
    $x[profit] = 20:

    foreach($x as $key = $value) {
        echo " $"+$key+" $"+$value;
    }
} 
?>

How many can you find?

If you got the missing comma in the parameter list, the “x = new Array();” error, the colon instead of a semi-colon, the ‘=’ instead of ‘=>’ in the foreach statement, and the erroneous use of ‘+’ on the echo line, then congratulations, you found all the fatal errors. You have the basic PHP technical skills to pay the bills.

That’s not how I answered the question though. I noted the fatal errors, obviously, but I went further than that. For instance, did you notice that there were no single quotes around the array indexes ($x[sales] and $x[profit])? That won’t cause a fatal PHP error, but it is a coding error! Did you also notice the use of double-quoted strings instead of single-quoted strings on the echo line? Or the usage of the opening PHP short tag?

2. String output. Which line of code do you think runs faster?

print “Hi my name is $a. I am $b”;
echo “Hi my name is $a. I am $b”;
echo ‘Hi my name is ‘.$a.’. I am ‘.$b;
echo ‘Hi my name is ‘,$a,’. I am ‘,$b;

the last one is actually the fastest operation. print is slower than echo, putting variables inline in a string is slower than concatenating them, and concatenating strings is slower than using comma-separated echo values! Not only does not-inlining your variables give you a performance boost, but it also makes your code easier to read in any editor that has syntax highlighting (your variables will show up in nice colors). The little-known use of echo as a function that takes a comma-separated list of values is the fastest of them all, since no string operations are performed, it just outputs each parameter.


not rare information, just a reminder:

‘ROLLBACK’ to cancel and ‘COMMIT’ to confirm changes.

scope:
public — methods can be called from any scope.
protected — methods can only be called from within one of its own or inheriting methods
private — methods can only be called from within one of its own class methods

mysql_num_rows($results) function returns the number of rows in a recordset.

$_REQUEST contains: $_COOKIE, $_GET, and $_POST variables

if you use $_REQUEST you have no guarantee that the data came from your
POST data, which leads to security holes in your script
vs.
There’s nothing magic about POST requests. They don’t have to come from a browser,
they can easily be sent from a script too using cURL or the http_request class
(pear) or plain old fsockopen(). If a hacker didn’t know how to use these methods
they wouldn’t be a hacker.

$$ means “the variable of the name in the variable”
$$ means the value of the variable

$name = “Joe”;
$$name = “Hello!”; # create a variable named $Joe, and give it the value of “Hello”
print $Joe;

You would see “Hello!” displayed.

useful with forms where many input fields can be expressed / saved in an array

array_walk() — Apply a user function to every member of an array
array_walk() will walk through the entire array regardless of pointer position.

If the array that array_walk() is applied to is changed, the behavior of this function
is undefined, and unpredictable.

fgets($file) is a PHP function that gets data from an external file line by line.

  fgets(file, length)   
  #     file           Required. Specifies the file to read from
  #           length   Optional. Specifies the number of bytes to read. Default is 1024 bytes.

ex: (loops through every file line)

  $file = fopen("test.txt","r");
  while(! feof($file))
    { echo fgets($file). "\n"; }
  fclose($file);

The fputs() function is an alias of the fwrite() function.
This function returns the number of bytes written on success, or FALSE on failure.

 
  fputs(file, string, length) 
  #     file                   Required. Specifies the open file to write to
  #           string           Required. Specifies the string to write to the open file
  #                   length   Optional. Specifies the maximum number of bytes to write

  $f = file("junk");
  $num = count($f) ; 
  $o = fopen('new-junk', 'w') ;
  for ($i=0, $i < $num, $i++)
     {
     $ln = strlen( $f[$i] );
     $halfstr = substr( $f[$i], 0, $ln/2 ); # halfstr = the 1st half of the line 
     fputs($o, $halfstr."\n", 1024); 
     }
  fclose($o); 
 

"self quiz"
from www.zend.com

Question 1
What does
echo count ("123");
print out?

Question 2
Which of the following snippets prints a representation of 42 with two decimal places?
A) printf("%.2d\n", 42);
B) printf("%1.2f\n", 42);
C) printf("%1.2u\n", 42);

Question 3
Given
$text = 'Content-Type: text/xml';

Which of the following prints 'text/xml'?

A) echo substr($text, strchr($text, ':'));
B) echo substr($text, strchr($text, ':') + 1);
C) echo substr($text, strpos($text, ':') + 1);
D) echo substr($text, strpos($text, ':') + 2);
E) echo substr($text, 0, strchr($text, ':');

Question 4
What is the value of $a?
$a = 123 == 0123;
A) True
B) False

Question 5
What is the value of $result in the following PHP code?

   function timesTwo($int) {
       $int = $int * 2;    
   }
   ...
   $int = 2;
   $result = timesTwo($int);
   echo '*'.$result.'*';

Question 6
The code below _____________ because _____________.

  <?php
     class Foo {
  ?>
  <?php
         function bar() {
         echo "bar";
         }
     }
 ?>

A) will work,  class definitions can be split up into multiple PHP blocks.
B) will not work,  class definitions must be in a single PHP block.
C) will not work,  class definitions must be in a single file but can be in multiple PHP blocks.
D) will work,  class definitions can be split up into multiple files and multiple PHP blocks.

Question 7
What will be the output of the following PHP code:

         echo count(strlen("http://php.net"));

Answers:
1(1); 2(B); 3(D); 4(B); 5(0); 6(B); 7(1)



there is an intro. 20 Question quiz at

http://www.w3schools.com/php/php_quiz.asp

to check your basic knowledge.

Comments are closed.

We try to post all comments within 1 business day