-
Somewhere in the certification guide there is a chapter dedicated to “streams”.
A small part of it represents the input, output and error streams from PHP. C/C++ has famous stdio.h library, but few people knows that input from keyboard is possible in PHP too.
In short, this can be achieved with PHP://STDIN, PHP://STDOUT and PHP://STDERR.
As this subject is panicking a lot of developers that are studding for ZCE, let’s get the confusion out. A stream represents a flow of information, just like when your reading and internal or external file using fopen.
But as a programmer best understands from cod, let’s get to more practical stuff.
Input
For input there is PHP://STDIN.
The next script is reading input from the keyboard:
1#!/usr/bin/php 2<?php 3// initializing the input stream 4$input = fopen('php://stdin', 'r'); 5 6// welcome message 7echo 'Type "exit" and then enter to exit' . PHP_EOL; 8 9// reading the stream input 10while($line = fgets($input, 1024)) { 11 // exit condition with line terminator 12 if($line == 'exit' . PHP_EOL) { 13 echo 'bye bye' . PHP_EOL; 14 break; 15 } 16 // displaying the input from the keyboard 17 echo 'You say: ' . $line . PHP_EOL; 18} 19 20// close stream 21fclose($input);The first line of code is specially for Linux/Unix, and Windows users can remove it.
The cod above must be placed in to a file, like for instance testStream.php.
You must have execution rights for that file, witch can be achieved with:
1chmod +x testStream.phpThen the file can be executed in Linux directly with:
1$./testStream.phpIn Windows you must give the absolute path to PHP, if the path is not already in the system include path:
1>c:\php\php.exe testStream.phpNotice that the input is with “\n” or “\r\n” at the end of the line, that’s why I’m testing “exit” with the line terminator (PHP_EOL). I’m using PHP_EOL so it can work with both Linux/Unix and Windows.
Output
For output there is PHP://STDOUT.
Unlike input, the output is a lot less relevant. Basically the output is the standard one which can be achieved with echo and print.
But for educational purpose let’s modify the previous file to use PHP://STDOUT:
1#!/usr/bin/php 2<?php 3// initializing the input stream 4$input = fopen('php://stdin', 'r'); 5 6// initializing the output stream 7$output = fopen('php://stdout', 'w'); 8 9// welcome message 10fwrite($output, 'Type "exit" and then enter to exit' . PHP_EOL); 11// reading the steam input 12while($line = fgets($input, 1024)) { 13 // exit condition with line terminator 14 if($line == 'exit' . PHP_EOL) { 15 fwrite($output, 'bye bye' . PHP_EOL); 16 break; 17 } 18 // displaying the input from the keyboard 19 fwrite($output, 'You say: ' . $line . PHP_EOL); 20} 21 22// close input stream 23fclose($input); 24 25// close output stream 26fclose($output);Basically there isn’t any change in the script, is just that the output was displayed using PHP://STDOUT explicitly.
Error
A more interesting subject then the output is the error stream.
Basically is more relevant in the Unix/Linux environment, probably is relevant in Windows too but I don’t know how to capture it. If your reading this blog and you know how to do this please leave a comment below.
And again let’s change the script so that error messages will use the proper stream. I’ll output an error each time the user enters more then 5 characters (I’m sorry but I don’t have a lot of ideas right now):
1#!/usr/bin/php 2<?php 3// initializing the input stream 4$input = fopen('php://stdin', 'r'); 5 6// initializing the output stream 7$output = fopen('php://stdout', 'w'); 8 9// initializing the error stream 10$err = fopen('php://stderr', 'w'); 11 12// welcome message 13fwrite($output, 'Type "exit" and then enter to exit' . PHP_EOL); 14 15// reading the steam input 16while($line = fgets($input, 1024)) { 17 // exit condition with line terminator 18 if($line == 'exit' . PHP_EOL) { 19 fwrite($output, 'bye bye' . PHP_EOL); 20 break; 21 } 22 23 if(strlen($line) > 5) { 24 fwrite($err, 'WARNING! Input greater then 5 characters: ' . $line); 25 continue; 26 } 27 28 // displaying the input from the keyboard 29 fwrite($output, 'Ai scris: ' . $line . PHP_EOL); 30} 31 32// close input stream 33fclose($input); 34 35// close output stream 36fclose($output); 37 38// close error stream 39fclose($err);By default in Linux error messages are displayed to the screen, but there are scenarios when the errors are redirected for instance to a log file .
To redirect the error messages to a log file 2>> is used like in the following example:
1$./testStream 2>>testStream.logInput from PIPE (|)
Let’s take the fallowing scenario: the output of a previous operation contains a serious of email addresses that are both valid and invalid. There should be two files: valid.txt with valid addresses and invalid.txt with invalid ones. The email addresses will be send to the script using pipe.
The list of email addresses will be simulated using a file called email.txt:
1valid_addres@yahoo.com 2another_valid@yahoo.co.uk 3invalid@y.c 4good@gmail.com 5invalid addres@hotmail.com 6fooThe name of the script will be emailTest.php:
1#!/usr/bin/php 2<?php 3// initializing the input stream 4$input = fopen('php://stdin', 'r'); 5 6// initializing error stream 7$err = fopen('php://stderr', 'w'); 8 9// unlike the previous cases, here is tested for the end of the file 10// because the input is not coming from keyboard 11while(!feof($input)) { 12 // the value is trimmed to remove line terminators 13 $line = trim(fgets($input, 1024)); 14 15 // test email address 16 if(filter_var($line, FILTER_VALIDATE_EMAIL)) { 17 // direct output, equivalent to php://stdout stream 18 echo $line . PHP_EOL; 19 } else { 20 // invalid addresses are redirected to the php://stderr stream 21 // to be intercepted later 22 fputs($err, $line . PHP_EOL); 23 } 24} 25 26// close input stream 27fclose($input); 28 29// close error stream 30fclose($err);For testing I’ll simulate the output of email addresses with the cat command:
1cat email.txt |./emailTest.php >valid.txt 2>invalid.txtNow the files valid.txt and invalid.txt from the current directory contain the proper values.
Processing like this is very useful for complex operations. Basically is an alternative to Shell Scripting (linux) or Batch Scripting (windows), which are not as flexible.
Script arguments
Often is useful to send direct arguments to a script to have, for instance, a different functionality.
Let’s change the previous scenario to receive the name of the file that contains the email address, as a script argument.
The script arguments are automatically stored in the $argv variable. Notice the first element of the array ($argv[0]) is the name of the script!
The modified example is as follows:
1#!/usr/bin/php 2<?php 3 4// the count stars at 1 to eliminate the name of the script 5for ($i = 1; $i < count($argv); $i++) { 6 // initializing the input stream 7 $input = fopen($argv[$i], 'r'); 8 9 // initializing error stream 10 $err = fopen('php://stderr', 'w'); 11 12 if(!$input) { 13 continue; 14 } 15 16 // unlike the previous cases, here is tested for the end of the file 17 // because the input is not coming from keyboard 18 while(!feof($input)) { 19 // the value is trimmed to remove line terminators 20 $line = trim(fgets($input, 1024)); 21 22 // test email address 23 if(filter_var($line, FILTER_VALIDATE_EMAIL)) { 24 // direct output, equivalent to php://stdout stream 25 echo $line . PHP_EOL; 26 } else { 27 // invalid addresses are redirected to the php://stderr stream 28 // to be intercepted later 29 fputs($err, $line . PHP_EOL); 30 } 31 } 32 33 // close input stream 34 fclose($input); 35 36 // close error stream 37 fclose($err); 38}Now just run the script with arguments:
1$./argTest.php email.txt >valid.txt 2>invalid.txt -
In 2008 when I had my first certification exam there were 22 Zend Certified Engineers in Romania, I was number 23.
With time looks like more people took the exam, and this week the number of 100 Zend Certified Engineers was exceeded!
After a search of PHP ZCE, full of joy, I find that the 100th certified web developer in Romania is actually my colleague and friend Emanuel Croitoru, whom I’ve tortured for weeks on end with: “don’t worry, is easy”.
When the time came to really study the manual it didn’t look that simple. 🙂
But after all, the determination to make this step is just another reason that a ZCE is trying to become a good web developer!
-
Ever since I’ve learned about the existence of the elephpant, I’ve wanted one. A goal not exactly romantic or worthy of being idealistic, but a lot more complicated than it looks.
Not even google can give very relevant results. The french company Nexen which is producing them don’t have any retail shops, in fact there isn’t any official retail market for them.
Although the Web is full of photos of the php elephant from all sorts of events, or even erotic ones, not the same thing can be said about places to buy if from.
The Nexus site states that there is a minimum of 50 small php elephants or 1 large, but here is where the story ends. At some point I decided to try to buy 50 elephant but… I was unable to do that either.
Eventually, after about 2 years of searching I’ve got by chance on eBay. I’ve thought that maybe a second hand one exists. It seems that there is somebody in US that is distributing! When I’m writing this blog it seems that not even that announcement exists, but with a little luck in few weeks will resurface. The search has to be on “php elephant” not “elephpant” which is the official name.
After about a couple of weeks since placing the order, it arrived! Now I’m the happy owner of an elephpant! The price is not exactly small, 15 pounds + delivery, but for this long awaited mascot I think it worth it.
It’s really quite small, but very nicely done, and very cute. He was my mascot from the ZCE 5.3 certification.
The mystery about the rarity of these elephants had an answer on elephpant.com: “No retail at the moment. We prefer relying on local PUG or online shop to actually do retail.”. It looks like at the moment the only way to get one is to find someone that bought 50 php elephants and now is selling some of them, or to buy 50 yourself!
The fate of the 2011 elephpants production it was decided at the end of last year, with a pre-order form on http://www.elephpant.com/. And most likely pre-orders will be soon available again.
When the pre-order form appeared on elephpant.com I even got a email telling me to pre-order if I’m still interested. So if you are interested you should subscribe to the newsletter.
With this generation a new pink php elephant is on his way! Probably they are here for the PHP Woman side of the community. There aren’t any images with this new elephant, because this is the first generation.
-
As I was saying in a previous blog, when Zend launched the new ZCE 5.3 certification I’ve received an discount voucher code, available until the end of year 2010. I wanted to use this opportunity, so in the last week of the year I’ve bought the voucher.
I already had the experience of the ZCE 5 certification, about which I’ve blogged at that time.
I wanted to take the exam before the end of the vacation, that is 7-I.
The period was a little short, in theory, I had about two weeks to prepare, but because they ware around the hollydays the time was in fact way shorter.
Zend PHP5 Certification Study Guide – 2nd Edition
I’ve begin my study with Zend PHP5 Certification Study Guide – 2nd Edition. The book even though it does not have the latest 5.3 features is not outdated. Because it wasn’t the first time I was reading it, it felt more like a recap. I’ve tried for each chapter to create an example that will show the functionality and one that shows the cases where the functionality was not as expected. With all this in 4 days I was able to go through the entire book.
As a suggestion, for streams and SPL for instance where is a little difficult to study directly form the manual, the guide looks like a good start.
Because after all the book is a guide, as I was reading the book, I’ve looked in the corresponding chapters in the php.net manual for a more detailed view on the subjects.
Mock tests
Before I finished reading the first book I’ve took a PHP5 mock test and to my surprise the result was “Excellent”. I got the mock tests from when I took the ZCE 5 exam, and because a lot of the questions are repeat quite frequently, I’ve only used 3 back then. This time I’ve only took a couple of them for the same reason. The tests are quite useful for the PHP5 part because, just like there ware described, they are usual more complicated then the exam itself. But don’t take to many of them, especially if the first results are poor, because they may lead you in a false self trust because of the repetitive questions.
Zend PHP 5.3 Study Guide
When I’ve finished reading the first book I started reading the free Zend PHP 5.3 Study Guide, which can be downloaded from the certification page, in the right. The guide is in beta version and you can really feel that. Everyone who’s been talking (blogging) about it is saying that it is full of bugs, and after all that is true. One of the funniest bugs in my opinion is at page 109, question 12, the answer is D… which is not displayed in the page. But I believe that where there are bugs they will be easily discovered and will not mislead.
It feels a lot more like a guide then the previous book, is a lot more concise and abstract and if forces you to study the manual.
At the end of each chapter there are questions, I only had 100% on a couple of chapters. The questions a quite difficult, even more difficult then the questions in the actual exam in my opinion. You have to read each question carefully because in the exam there are the same type of questions.
To go through the entire guide it took me another couple of days because I was already warmed up from the precedent book.
The day before the exam
I’ve read on Lorna “lornajane” Mitchell‘s blog that before the exam is good to recap some delicate subjects. In the day before the test I’ve started to recap some subjects that need more memory then intellect, like the tens of functions for string si array manipulation. Also I think is a good idea to recap SPL.
The exam it self
After not a lot of studding the moment of the exam was here.
Just like the last time, I’ve schedule my exam over the Internet a day before and just went to the Pearson VUE center.
The exam itself didn’t feel more difficult then the PHP 5 one. In my opinion there is more focus on high level OOP and SPL then the previous exam. The style of the questions is quite similar, maybe a little more “type in” questions, but that can be just my luck because the questions are dynamically selected from a question pool with different difficulty levels.
The 90 minutes time has quite enough to go through all the questions whit all the attention required and to review the answers I wasn’t sure about. At the ZCE5 exam I’ve finished about a half of hour before the time. This time for ZCE 5.3 I’ve finished only 10 minutes early because I’ve tried not to rush at all.
READ CAREFULLY! Is very important to read each question carefully and read in again until you are sure you understand what is required of you. Some questions have a quite awkward formulation and can mislead very easy. Don’t panic, if you have doubts maybe you should mark the question for review and come back to it in the end.
I usually mark the first question of the exam for review because I’m nervous and I can’t focus enough.
Unlike 2 years ago, when I was working a lot more with raw PHP and even had some PHP 4 servers, last year I’ve worked most of the time with open-source frameworks. This can be a disadvantage because I’ve didn’t use just as much the core functions. But unlike the first certification I must admit I didn’t study as much, probably is because of experience and the fact that occasionally I flick through the pages of the guide.
And with that being said it is time to put the first check on the year resolution and add a new logo to my blog.
For those who are preparing, I wish you the best of luck!
-
Another year has passed and PHP 6 will not arrive.
Actually for the last couple of years I’ve been expecting PHP 6, and I believe this is more of a traditional blog for me to close the year.
PHP 6 will not come because the trunk was abandoned and rebuild from a 5.3.
Even if PHP 6 will not be out for a while now, PHP 5.3 is gaining popularity. Frameworks like Zend Framework and Symfony are each preparing in 2011 a 2.0 version that will need PHP 5.3+ to run. Even CodeIgniter, a framework that is was traditional PHP 4 compatible will need at least PHP 5.1.6 for version 2.0.
Even more, the official Zend certification for PHP is now ZCE 5.3, and it’s becoming very popular, even if it was released just few months back.
But a new year is ending and is time to check achievement from last year’s resolution and to write a new one.
Last year I’ve finished my masters, and with it I’ve finished way to many years to want to count of school. In fact this is the firs winter that I’m not in some form of school, maybe this is why I feel like I have so much free time :).
I’ve change my work place, and with it I was forced to use some things that were on my TODO list for quite some time:
- Linux
- Symfony Framework Linux was a subject around I was gravitating inevitable for years, but never got to in to deep. It was always on my “todo” list, but never got the time or patience to really get into much detail with it, or didn’t have the continuity when I did it. When I got to my new work place I found my self in front of a Ubuntu computer, and I’ve started to panic a little.
After few months I’ve made a new step, and for the first time in my life I went to a professional course. The course was organised by Info Academy. Paid from my own pocket and a little overlapped with my work hours. But I’ve reached the conclusion that I had to do it. Probably it does not sound like a big deal, but I usually study on my own, and it was quite weird for me. Now that is over I can say that it was a great investment and I recommend it (to all from Romania that can go to this centre).
Sometime during that course I’ve realised that it was time to boot in linux from time to time even at home. The next step was to reconfigure the boot manager to boot directly to linux. Now that I’m almost exclusively use linux, I’ve reached the conclusion that is user friendly enough to be a real alternative to Windows.
Symfony framework is another dusty entry on my “todo” list. Even if I’ve been playing around before with CodeIgniter and Zend Framework, I’ve never even got near to Symfony until last year. I’ve felt the fundamental difference of concept between ZF and Symfony. This was another reason to panic at my new work place, and after all: “all frameworks suck”.
Is not a very easy to learn framework because of the concepts, but I thing it worth it. What I like most about this framework is the CRUD generating, that is very easy to do but is very powerful and flexible. Another thing is the use of YML files, which was taken from Ruby on Rails, is a way better alternative to the PHP’s native ini files.
In the end, 2010 was a good year, with lot’s of achievements, even if I didn’t check everything on my last year’s resolution, I think I’ve checked enough.
And now I wish you a great and full of achievements 2011! Happy new year!
