Awesome q2a theme

Why not work in case compare zero and one?

0 like 0 dislike
55 views
Study, was given the task of not pre-specifying some of the details.
Explain the essence of the question in comments in the code

spoiler
<?php//объявляем переменные$peremennaya1=2; //присвоили числовое значение$peremennaya2=2; //присвоили числовое значение$peremennayapolz=$_GET['peremennayapolz']; // переменную должен задать пользовательswitch ($peremennayapolz) {case $peremennayapolz < $peremennaya1:  //  работает если пользователь задает значение переменной "1", а если задает значение "0" то почему то выводит значение, что переменная пользователя равна переменной1 (0=1) как???echo "Задуманное число не входит в числовой ряд";break;case $peremennaya1 == $peremennayapolz: // все работаетecho "Переменная пользователя равна переменной 1<br>"; break; case $peremennayapolz > $peremennaya1: // everything works echo "user Variable is greater"; break; case $peremennayapolz == null: // Google having studied and having estimated decided it would be necessary to check for variable value and it's not working. echo "Set variable"; break; } ?>

by | 55 views

4 Answers

0 like 0 dislike
Read ka php.net/manual/ru/control-structures.switch.php and note examples.

This statement compares the switch (what) and case (like here) between them. The use is simply not correct.

If $peremennayapolz you have zero, in the second case, a comparison of this zero with the result of the expression
$peremennaya1 == $peremennayapolz. The expression yields false, and false == 0.

As for me, it is better to rewrite the if elseif php.net/manual/ru/control-structures.elseif.php

As an option to cheat and write switch (true) ...
by
0 like 0 dislike
//declare variables $peremennaya1=2; //assigned numeric value $peremennaya2=2; //assigned numeric value if (isset($_GET['peremennayapolz'])){ $peremennayapolz = intval($_GET['peremennayapolz']); switch ($peremennayapolz) { case $peremennayapolz < $peremennaya1: echo "Conceived the number is not included in the numeric range"; break; case $peremennaya1 == $peremennayapolz: // everything works echo "Variable a user variable is equal to 1"; break; case $peremennayapolz > $peremennaya1: // everything works echo "user Variable is greater"; break; default: echo "Set variable"; break; } }
by
0 like 0 dislike
1. Type cast intval()
2. Comparison of types: php.net/manual/ru/types.comparisons.php
by
0 like 0 dislike
Case works on comparison sort.
Ie
switch ($peremennayapolz) {
case 1:
echo "Conceived the number is not included in the numeric range";
break;
case 2 // all works
echo "Variable a user variable is equal to 1";
break;
case 3: // all works
echo "user Variable is greater";
break;
default:
echo "Set variable";
break;
}
}
Then suddenly, the comparison in the place where should be the value?.... there will be either 0 or 1. (comparison result)
by

Related questions

0 like 0 dislike
2 answers
asked Apr 13, 2019 by Senseich
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users