Welcome to the new installment of my series of Coding Quizzes, in which you will be able to test your knowledge and skills about programming and software development in a simple and fun way. If you want to learn more about it visit my blog here on Hive and the first post where I introduced it.
Solution to the previous quiz: int(3). The code is quite self-explanatory for PHP programmers. They know that a bloddy overlooked semicolon can cause problems in this language.
In an interactive PHP session, lines 1 and 2 are omitted, because line 1 has a syntax error that is actually detected on line 2. The interpreter will tell you that $var1 does not exist and that is because we did not add a semicolon to line 1. As a consequence, the interpreter will execute lines 3 and 4, which are valid code.
Line 3 introduces the ?? operator called Null Coalescing Operator. It is new in PHP 7 and what it does is simplify the old use of the ternary operator in conjunction with the isset function to determine if a variable exists and is not null. Thus, if the first variable exists and is not null, it will be chosen, otherwise, the second is chosen.
In our code, since $var1 does not exist, the null coalescent operator will choose 3 and this value will be assigned to $var2.
The final line uses the var_dump function which simply prints the contents of the given variable and its data type. This produces the output that is int(3).
The hiver @dobro2020 has solved this quiz perfectly. Congratulations.
If you want to blog about computer science and programming content, I invite you to join Hive and participate in its communities, such as STEM-social, Develop Spanish, Programming & Dev and others.
Bienvenido a mi nueva serie de Quizzes de Programaci贸n, en la cual podr谩s poner a prueba tus conocimientos y habilidades sobre programaci贸n y desarrollo de software de una manera sencilla y divertida. Si quieres aprender m谩s sobre ella visita mi blog aqu铆 en Hive y el primer post donde la present茅.
Soluci贸n al quiz anterior: int(3). El c贸digo es bastante autoexplicativo para los programadores de PHP. Estos saben que un bendito punto y coma pasado por alto puede causar problemas en este lenguaje.
En una sesi贸n interactiva de PHP, las l铆neas 1 y 2 son omitidas, por cuanto la l铆nea 1 posee un error de sintaxis que realmente es detectado en la l铆nea 2. El int茅rprete te dir谩 que $var1 no existe y eso se debe a que no colocamos punto y coma a la l铆nea 1. Como consecuencia, el int茅rprete pasar谩 a ejecutar las l铆neas 3 y 4 que s铆 son c贸digo v谩lido.
La l铆nea 3 presenta el operador ?? llamado Operador coalescente nulo. Es una novedad de PHP 7 y lo que hace es simplificar el antiguo uso del operador ternario en conjunci贸n con la funci贸n isset para determinar si una variable existe y no es nula. As铆, si la primera variable existe y no es nula, se escoger谩, sino, se escoge la segunda.
En nuestro c贸digo, por cuanto $var1 no existe, el operador coalescente nulo escoger谩 3 y este valor se asignar谩 a $var2.
La l铆nea final usa la funci贸n var_dump que simplemente imprime el contenido de la variable dada y su tipo de dato. Con ello se obtiene la salida que es int(3).
El hiver @dobro2020 ha resuelto este quiz perfectamente. Felicidades.
Si quieres bloguear sobre contenido inform谩tico y de programaci贸n, te invito a unirte a Hive y participar en sus comunidades, tales como STEM-social, Develop Spanish, Programming & Dev y otras.