What would the value of the variable $num be after the following code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What would the value of the variable $num be after the following code?

<?php $num = 9; $num = $num%2; ?> Please give examples to understand it.

12th Sep 2017, 1:28 AM
Hima
Hima - avatar
14 Answers
+ 3
1
27th Apr 2021, 4:48 PM
IMTIAZ AHMAD
IMTIAZ AHMAD - avatar
+ 2
% is mod or modulo in algebra basic ? then it means var 9 keeps deducted by 2 until leftover of 1 , and won't go below 0 or minus : 9 - 2 = 7 , 7 - 2 = 5 , 5 - 2 = 3 , 3 - 2 = 1. mod-ing by 2 usually for check odd / even number. unless mod-ing different number like 90 % 13 : 90 - 13 = 77 , 77 - 13 = 64 , 64 - 13 = 51 , 51 - 13 = 38 , 38 - 13 = 25 , 25 - 13 = 12. result is 12.
12th Sep 2017, 1:40 AM
Mikhael Anthony
Mikhael Anthony - avatar
+ 2
1
18th Oct 2021, 8:24 PM
Hassiani Abdesselem
Hassiani Abdesselem - avatar
+ 1
The answer is 1
24th Jun 2019, 1:27 PM
adejumo olusanya
+ 1
What would the value of the variable $num be after the following code? <?php $num = 9; $num = $num%2; ?> answer is 1
23rd Apr 2020, 6:03 PM
urvi patel
urvi patel - avatar
+ 1
Porque la respuesta es (1)????
25th Aug 2022, 4:47 AM
Norberto Ramirez
Norberto Ramirez - avatar
+ 1
Norberto Ramirez This is the first impression if there is no goal for learning, but with time and repetition and "repetition" the matter becomes simple. A person in his childhood stage cannot walk from the first time, but with repetition and effort, something impacts more. at once, he can walk a greater distance and so on until he is able to run more and more distances. --------------------------------- Esta es la primera impresión si no hay una meta para el aprendizaje, pero con el tiempo y la repetición y la “repetición” el asunto se vuelve simple. Una persona en su etapa infantil no puede caminar desde la primera vez, pero con la repetición y el esfuerzo, algo impacta más. de una vez, puede caminar una distancia mayor y así sucesivamente hasta que sea capaz de correr más y más distancias.
28th Aug 2022, 5:48 PM
Hima
Hima - avatar
0
it equals 1. it's very simple num is set to equal 9. at this point num equals 9. Now pay attention. unlike English which is parsed from left to right, code is parsed from right to left this means that before we can initialize the variable num, we must evaluate everything in the right side first we evaluate num which we know is 9 Now our equation looks like this: $num = 9%2; ö as you probably know the modulus (%) operator divides a into b and returns the remainder. so 4%2 = 0 because 4/2 = 2 with no remainder whereas 5%2 = 1 because 5/2 = 2 remainder 1. we are looking at the remainder. therefore 9%2 means dividing 9 into 2 parts of 4 with 1 left over. this also means that 9%2 = 9%4 = 1 Now once we have evaluated the equation on the right we get to the code on the left and set the new value of $num to 1. hope this helped
12th Sep 2017, 1:56 AM
Adam
Adam - avatar
0
@tony @eitan brezes Of course, this told me a lot, especially I was thinking about when we can used it and for what. Thank you so much guys.
12th Sep 2017, 1:56 PM
Hima
Hima - avatar
0
it equals 1. it's very simple num is set to equal 9. at this point num equals 9. Now pay attention. unlike English which is parsed from left to right, code is parsed from right to left this means that before we can initialize the variable num, we must evaluate everything in the right side first we evaluate num which we know is 9 Now our equation looks like this: $num = 9%2; ö as you probably know the modulus (%) operator divides a into b and returns the remainder. so 4%2 = 0 because 4/2 = 2 with no remainder whereas 5%2 = 1 because 5/2 = 2 remainder 1. we are looking at the remainder. therefore 9%2 means dividing 9 into 2 parts of 4 with 1 left over. this also means that 9%2 = 9%4 = 1 Now once we have evaluated the equation on the right we get to the code on the left and set the new value of $num to 1. hope this helped Thanks seeandpost.wordpress.com seeandpost.blogspot.com
23rd Feb 2018, 5:30 PM
Abdullahi Opeyemi
Abdullahi Opeyemi - avatar
0
lol u copied my post?
24th Feb 2018, 6:27 PM
Adam
Adam - avatar
0
Norberto Ramirez After a lot of research, l found the easiest and fastest way to solve any example of this as follows: We divide the two numbers 9/2 = 4.5 and the result is deleted from it any number from the fractions or after the point if 9/2 = 4 Then the result is multiplied by the number that was done Divide by 4 x 2 = 8 then subtract 9-8 = 1 You can try this at any example and I'll translate this for you to Spanish and please forgive me for any mistake, I'm not good at Spanish ------------------------------------------------------ Después de mucha investigación, encontró la forma más fácil y rápida de resolver cualquier ejemplo de esto de la siguiente manera: Dividimos los dos números 9/2 = 4.5 y el resultado se elimina de cualquier número de las fracciones o después del punto si 9 /2 = 4 Luego el resultado se multiplica por el número que se hizo Divide por 4 x 2 = 8 luego resta 9-8 =1
28th Aug 2022, 4:21 PM
Hima
Hima - avatar
0
Soy nuevo en programación a pesar que tengo algo de lógica pero matemáticamente no le veo lógica para qué se usa esa en programación porque no le consigo sentido todavía
28th Aug 2022, 5:21 PM
Norberto Ramirez
Norberto Ramirez - avatar
0
👍🏻👍🏻👍🏻
29th Aug 2022, 3:42 AM
Norberto Ramirez
Norberto Ramirez - avatar