My Coding Quiz #27

in #hive-19638711 months ago

My Coding Quiz #27 👨‍💻🛠️🧩

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.

Without further ado, here's the riddle...




Quiz
By @eniolw


What's your choice?

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.


Mi Quiz de Programación #27 👨‍💻🛠️🧩

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é.

Sin más preámbulos, he aquí el acertijo...




Quiz
Por @eniolw


¿Cuál es tu elección?

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.

Sort:  

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

Que cosas raras hace php jajajaj