Php short if fails [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a little problem with "short if" in Php.
isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value
this drops notices. It looks the $this->sets[$this->value] runs even when it doesnt exists. If I do:
if (!isset($this->sets[$this->value]))
{
$this->sets[$this->value] = '';
}
it does solve the problem, but then I dont understand something....
EDIT: I refactored:
if (isset($this->sets[$this->value]))
{
$value = $this->sets[$this->value];
}
else
{
$value = $this->value;
}
return $value;
and it works, dunno why....

return 'something'.isset($this->sets[$this->value])?$this->sets[$this->value]:$this->val‌​ue;
'something'.isset($this->sets[$this->value]) always evaluates to true. You'll need to group the ternary operator expression:
return 'something' . (isset($this->sets[$this->value]) ? $this->sets[$this->value] : $this->val‌​ue);
And that's why you always post a complete example in your question, not just a subset!

It is not true that both operands are evaluated. Try this to see:
true?print('1'):print('2');
Only the '1' prints.
The issue is that your first line of code does not do anything in and of itself. You don't assign the result of the expression to anything, you don't use it anywhere, I would not be suprised if zend just discards it.
In your second example, you explicitly create an array element if it does not already exist. If you wanted to do the same thing with the ternary operator, you could do
isset($this->sets[$this->value])?null:($this->sets[$this->value]='');
I do not know why you would want to, but it would achieve the same thing as you did in your second example.
Your refactored example can be accomplished using the ternary operator as:
return isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value;
This is a typical usage of the ternary operator

Related

if($_GET['info'] ==...) not checking [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wrote a little check to display succes messages whenever a form is validated succesfully. However the check I use doesn't work. it doesn't matter what $_GET['info'] is, it just shows all messages unless $_GET['info] is empty. so even when info = loggin it shows all three messages. any help?
if(!empty($_GET['info'])){
if($_GET['info'] == "succes-paid"){
echo "<p class='succes'>De bestelling/inschrijving is succesvol verlopen.</p>";
}
if($_GET['info'] == "succes-register"){
echo "<p class='succes'>U werd succesvol geregistreerd.</p>";
}
if($_GET['info'] == "login"){
echo "<p class='succes'>U werd succesvol ingelogd.</p>";
}
}
You have a semi-colon after each of your if statements. The semi-colon means "finish the statement". Therefore your program thinks that you are done with the if and everything in your braces is treated as a block separate from your if statement. That is why those blocks are always executing.
It's because of the semicolon right after the condition of the if.
That line is actually two statements. The first is the if with condition, followed by an 'empty' statement, ended by a semi-colon. The second is a compound statement (a block of statements within a set of { .. }. The compound statement doesn't have a condition at all, so it is always executed.
if($_GET['info'] == "succes-paid") ; {echo 'x'}
condition with no statement -----^ ^ ^--------^-- compound statement without condition
\_The semi-colon that separates them.
And you have this situation three times, hence three lines of output.

$_GET["id"] don't respond [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am a beginner in php , and i want make a test with GET .
I want display , for example "ok" on my php page if my id parameter is 1 but i have always 1 when i change the id parameter to another value .
Details :
when i make this url :
http://localhost:81/test/testajax.php?id=2
expected result :
not ok
obtained result :
ok
testajax.php
<?php
if($_GET["id"] = 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
One equal sign (=) sets the value of a variable. $foo = "bar"; would set $foo to store bar.
You want to use two equal signs (==), which is a comparison operator. ($foo == "bar") would check to see if $foo is equal to bar.
You can check the different types of operators at http://php.net/manual/en/language.operators.php
May be you should go through this basics before you start.
you should put two equal signs to compare.
if($_GET["id"]==1)
correct code:
<?php
if($_GET["id"] == 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
As you are setting the variable, the if statement is always equating to true
Just thought it was worth Noting this as that is the logical reason for your issue

Ternary Operators and exit statements [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hey everyone I have this one line of code, and I was thinking about something, is this considered a legal assignment of a variable or will it cause errors. Furthermore is it okay to use exit() statements like this, or am I just terrible at coding somedays? Also if there is a duplicate question like this, please point me in the right direction that would be fanastic!
list($foo, $bar) ? generateValues($data) : exit("Unable to obtain useful information);
The list() you are using will assign $foo and $bar values if you use it like so:
list($foo,$bar) = array('fooValue', 'barValue');
so to properly use it in a tertiary statement would be like so:
list($foo, $bar) = (conditional) ? generateValues($data) : exit('...');
the exit will fire if the conditional is false, otherwise the array generated by generateValues() will be returned by the assignment, and list() will assign the values respectively.
Documentation.

function split() deprecated - besides preg_split what else needs to be changed in this code? general idea [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I know that there are several questions about this issue, but this is a particullar case...
In the following code (in the the first and the last line) I have to replace split with preg_split, but I think something else needs to be changed too.
Please tell me what should I change in the code for it to work, and the theory behind this change, i.e. the gereral idea behind switching between split and preg_split. The code which needs the transition is:
$opt = split("_",$key);
if($opt[0]=="id" && $val!="0" && $val!=""){
some queries
$shuffle=split("_",$_POST['all_'.$i]);
Use explode instead of split. Your code should look like this :
$opt = explode("_",$key);
if($opt[0]=="id" && $val!="0" && $val!=""){
some queries
$shuffle=explode("_",$_POST['all_'.$i]);
Documentation : http://fr2.php.net/explode
PHP is in the process of dropping an older POSIX-compatible regex extension in favour of the newer PCRE extension. This means that older functions like split() and ereg() will be removed in time.
The PCRE equivalent for split() is preg_split(), which has a modified syntax. For your code you'd use:
$opt = preg_split("/_/",$key);
However, a Regex function is a heavyweight tool and isn't required here. You just need explode(), like this:
$opt = explode("_",$key);

"Indirect" isset() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
function myCheck($in)
{ return isset($in); }
$var1='Something';
$var2='$var1';
$var3='$varNonExitant';
What I'm trying to achive is to use myCheck to evaluate the existance of the content like this:
myCheck($var2) return true;
myCheck($var3) return false;
isset() is not really a function: it's a language construct. As such, it's allowed to do some magic that's not available to regular functions, such as being fed with non-existing variables.
To sum up: you cannot replicate it with a custom function.
Edit:
As DaveRandom pointed out in a comment below, all you can do is come close by checking if a variable isset for example:
function variable_isset(&$variable = NULL) {
return isset($variable);
}
This approach offers two drawbacks though:
This works by passing the unset variable by reference, thus creating it when called. As it's NULL it is still not set.
It'll trigger an Undefined variable notice if the variable does not exist, ruining the whole concept of gracefully handling optional variables.
Most likely this should not be needed. So question remains why you can not use isset in the first place which would be much more needed to give you better guidance.
When you cyall myCheck($abc), with set $abc = 123, it gets myCheck(123). It isn't any valid argument for isset.
You have to give the function a string with variable name:
function isset_global($variable_name)
{
return isset($GLOBALS[$variable_name]);
}
Of course, I am also wondering, why to do this, but it answers the question as long as you check a global variable.

Categories