Day 1 – PHP Basics

Zend PHP Certification

Before I start with my progress, I’d love to share an awesome command I wrote the other day.  I’m writing some intensive PHP scripts for my job that need to be run in the background and on multiple threads to increase speed and to provide the user with an asynchronous experience.  I was going to use PCNTL, but it wasn’t compiled into the system I was on, so I ended up spawning off tasks as separate background scripts.  The problem I ran into was that the website would grind to a halt because the scripts (usually 5 active at a time) would run on all four cores and take up 100% of the CPU.  So what do I do?  I need to run these scripts, but I need the website to continue to be operational.

My first choice was to play with the nice level of the process “proc_nice()”  That worked at first, until I started dealing with 20-30 scripts at once.  Then the *nix box decided to just throw EVERYTHING to the lowest priority just to keep the server up.  Then I realized, I have four cores, why take up every core?  After some research, I came across the “taskset” command which allowed me to specify which core my process will run on.  Take a look at the following code:

exec(“taskset -c “.($i % 2)”; php theScript.php > /dev/null 2>&1 & echo $!”, $opt);
  1. <p>Assuming we’re in a loop with $i as the index, this will spawn the intensive processes into the background and balanced on the first two cores only.&#160; Also, if you read in “$opt[0]” you can return the pid of your new running process (if you needed to kill it later).&#160; Anyway, I was really proud of this line of code and thought I’d share it.&#160; Back to the studying.</p>
  2. <hr />
  3. <p>The first chapter is all about PHP Basics.&#160; And as a PHP programmer for ~6 years, it shouldn’t be anything new.&#160; Here are some interesting things I didn’t know:</p>
  4. <p><span id="more-396"></span></p>
  5. <ul>
  6. <li>You can use <strong>asp tags</strong> <% %> instead of<strong> standard tags</strong>
  7. <php ?> via a configuration option. “Nobody quite understands why ASP tags were introduced” – Study Guide </li>
  8. <li>The first newline after the closing tag ?> is <strong>ignored by the parser</strong> to prevent any extra data being sent to the browser.&#160; Imagine you were outputting a binary file and you sent an extra /n/r.&#160; That could cause a lot of trouble. </li>
  9. <li>In the same context, <strong>omitting the closing tag</strong> ?> is perfectly legal. </li>
  10. <li><strong>Comments</strong> are done by //, # (did not know that) and /*&#160; */ (multiline).&#160; However, if you put a closing tag ?> in a comment:
  11. <pre lang="php">// This is a comment
  12. // This is a closing tag ?> did you know that?

Then the browser would actually output " did you know that?" because it would stop parsing PHP after the ?> Even inside a comment block.  Hmm...

  • “echo” is NOT a function, it’s a “language construct statement” and has no return value.  If you wanted something similar that was a function, use “print”  It does the same thing and always returns 1.
  • There are two data types in PHP:
    • Scalar:
      • boolean (true or false): number to boolean = false if == 0, true otherwise.  string to boolean = false if == ”0” or “”, true otherwise.
      • int (signed integer): standard decimal, octal signified by leading zero (0755), and hexadecimal signified by leading 0x or 0X.
      • float (signed floating-point): standard decimal and exponential signified by e or E (2e10).
      • string (collection of binary data).
    • Compound:
      • Arrays: containers of ordered data elements.
      • Objects: containers of both data and code.
  • All variables must start with either a letter or an underscore!
  • Variable Variables are awesome!!  Example:
    $test = "cheese";
    1. $$test = "moon";
    2. echo $cheese; // outputs "moon"

    This is just plain crazy as you can, theoretically, create any variable name you want.  Even a variable called $123456.  You would have to access it using curly braces ${123456}, but you could still use it.

  • Variable Variables also applies to functions.  In the previous example, $test() would execute cheese().  Strange stuff.
  • I’m going to end just before Bitwise Operators, as I’m going to need to sit down with a clean piece of paper before I attempt to tackle them.  Until tomorrow.

    No Comments

    Leave a Reply

    Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



    • Donate

      If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

    • License

      Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.