PREPARATÓRIO CERTIFICAÇÃO PHP5 40 Horas Instrutor: Antonio Itamar Júnior STRINGS AND REGULAR EXPRESSIONS - Declaração de classe - Nomenclatura - Instanciar classe - Utilizar atributos e métodos de classe STRINGS AND REGULAR EXPRESSIONS STRINGS ARE THE SWISS-ARMY knife of PHP, particularly if you consider the fact that most PHP scripts are used to serve web pages—which are, for the most part, nothing more than large strings. Knowing how to use this particular facility is, therefore, one of the most fundamental skills of the PHP developer, since you’ll be working with them day in and day out. Luckily, its essential role in the life of a PHP script has resulted in string manipulation being made exceptionally easy by the PHP development team. Therefore, once you get past the first few hurdles, handling strings becomes easy, quick and—to some extent—even fun. However, this particular aspect of PHP programming is far from being free of pitfalls. This portion of the exam tests your understanding of strings as well as your knowledge of the body of functions that are used to manipulate them. Additionally, you’ll find yourself faced with the basics of that most voodoo art of writing regular expressions, which, despite being a hugely useful tool, is too often neglected by most developers. 3 QUESTÃO 1 Consider the following script. What line of code should be inserted in the marked location in order to display the string php when this script is executed? “Considere o seguinte script. Qual linha de código deve ser inserida no local marcado para exibir a string php quando esse script for executado?” <?php $alpha = 'abcdefghijklmnopqrstuvwxyz'; $letters = array( 15 , 7 , 15 ); foreach( $letters as $val ) { /* What should be here */ } ?> A. echo chr($val); B. echo asc($val); C. echo substr($alpha, $val, 2); D. echo $alpha{$val}; E. echo $alpha{$val+1} 4 QUESTÃO 2 Which of the following will not combine strings $s1 and $s2 into a single string? “Qual das seguintes variáveis não irá combinar strings $s1 e $s2 em uma única string?” A. $s1 + $s2 B. "{$s1}{$s2}" C. $s1.$s2 D. implode('', array($s1,$s2)) E. All of the above combine the strings Explicação Com exceção da resposta A, todas as repostas produzirão o resultado desejado de concatenação das strings $s1 com $s2. No PHP, o operador mais (+) não combina duas strings juntas, como acontece em outras línguas, tais como Java e Javascript. 5 QUESTÃO 3 Given a variable $email containing the string [email protected], which of the following statements would extract the string example.com? “Dada uma variável $ email contendo a string [email protected], qual das seguintes declarações seria extrair a seqüência example.com?” A. substr($email, strpos($email, "@")); B. strstr($email, "@"); C. strchr($email, "@"); D. substr($email, strpos($email, "@")+1); E. strrpos($email, "@"); Explicação A funçao substr é usada para retornar uma parte de um texto, enquanto a função strpos é usada para encontrar uma determinada substring dentro de uma string. Utilizado em conjunto, podemos extrair a informação exigida. É importante perceber que é ex-zero-indexados, enquanto o último não é, 1 exigindo uma compensação a ser adicionado. É por isso que a resposta D é correto. 6 MANIPULATING FILES - Manipulação de Arquivos - 20 questões MANIPULATING FILES THOUGH YOU MAY NEVER think of file manipulation as one PHP’s strengths, it is actually a very useful tool for the developer. Even if you only develop websites, being able to read from and write to a file can turn out to be a very handy capability. After all, remember that, thanks to its stream wrappers (covered in more detail in Chapter 10), PHP makes it possible to open a remote file and read from it—useful, for example, for including content from a third-party site. On a more basic level, however, file input/output can be used for a multitude of tasks. For example, it’s handy for reading and interpreting the contents of a preformatted file, such as you could receive from a third-party provider sending you syndicated content, or for opening up and outputting binary files to the browser through your scripts so that you can more tightly control access to them. Whatever the end use, you will be tested not only on the basics of opening, closing and accessing a file’s contents, but also on fundamental aspects of file manipulation that are relevant in a multi-process environment, such as file locking. 8 QUESTÃO 1 The _______ function is used to read a single line from a file and is used when dealing with text files. For reading binary data or other specific segments of a file, you should use the _______ function instead. “_______ A função é usada para ler uma única linha de um arquivo e é usado quando se lida com arquivos de texto. Para leitura de dados binários ou outros segmentos específicos de um arquivo, você deve usar o _______ Função vez.” A. fgets(), fseek() B. fread(), fgets() C. fputs(), fgets() D. fgets(), fread() E. fread(), fseek() 9 QUESTÃO 2 Although file resources will automatically be closed at the end of a request in PHP, you can close them explicitly by calling the _______ function. Your Answer: ____________________________ 10 QUESTÇÃO 3 Consider the following PHP script, which reads a file, line-by-line, from a text file. Which function call should be inserted in place of the question marks in order for the script to function correctly? 11 QUESTÃO 4 Which of the following techniques will guarantee a lock safe from any race condition? 12 REFERÊNCIA BIBLIOGRÁFICAS PHP http://www.php.net Zend.PHP.5.Certification.Study.Guide.2006 The_Zend_PHP_Certification_Practice_Test_Book MySQL http://www.mysql.com [acessado em 1 de maio de 2009 as 21Hrs] http://dev.mysql.com [acessado em 1 de maio de 2009 as 20Hrs] http://www.w3schools.com/sql [acessado em 1 de maio de 2009 as 21Hrs] 13