Backticks, EOL, and Break

While studying up for the Zend PHP Certification exam I came across a few more elements that I’ve never used before.

Firstly, there is a backtick operator. This makes it possible to execute a shell command and retrieve
its output. For example,the following will cause the output of the UNIX ls command to be stored inside $a:

  1.  
  2. $a = ‘ls -l‘;
  3.  

Secondly, PHP has an end of line constant. I’ve never seen PHP_EOL before but it seems to work similar to “\n”.
I don’t think I will ever use this due to the length when compared with “\n”.

Finally, a bit about the break function. I had no idea that both the break and continue statements take a
paramenter which can instruct them to break out of multiple loops. I felt a bit silly that I didn’t already know this.
Here is a simple example:

  1.  
  2. for ($i = 0; $i < 10; $i++) {
  3.         for ($j = 0; $j < 3; $j++) {
  4.                 if (($j + $i) % 5 == 0) {
  5.                         break 2; // Exit from this loop and the next one.
  6.                 }
  7.         }
  8. }
  9.  

There seems to be a lot that I’m going to have to study up on before I can become a Zend Certified Engineer.

2 comments

  1. Bob says:

    PHP_EOL depends on the platform I think. On #nix systems it’s \n but on Wibdows it can be \r\n etc

    • admin says:

      You are definitely correct. I found this on stackoverflow:
      From main/php.h of PHP version 5.3.6:

      #ifdef PHP_WIN32
      # include “tsrm_win32.h”
      # include “win95nt.h”
      # ifdef PHP_EXPORTS
      # define PHPAPI __declspec(dllexport)
      # else
      # define PHPAPI __declspec(dllimport)
      # endif
      # define PHP_DIR_SEPARATOR ‘\\’
      # define PHP_EOL “\r\n”
      #else
      # if defined(__GNUC__) && __GNUC__ >= 4
      # define PHPAPI __attribute__ ((visibility(“default”)))
      # else
      # define PHPAPI
      # endif

      #define THREAD_LS
      #define PHP_DIR_SEPARATOR ‘/’
      #if defined(__MacOSX__)
      #define PHP_EOL “\r”
      #else
      #define PHP_EOL “\n”
      #endif
      #endif

      As you can see PHP_EOL can be “\r\n” (on Windows servers), “\r” (on MacOSX servers) or “\n” (on anything else).

Leave a Reply

Your email address will not be published. Required fields are marked *

*