php - issue with booleans in if statement - php

I have been running into a weird issue for some time with php now and wonder if anybody can help. It's probably a totally minor stupid thing I don't see.
"if" statements don't seem to work for me when I use && and || for logical expressions.
Latest example:
$isNotSet = !isset($moved); // var_dump prints false
$moveSuccess = $moved instanceof SuccessNotification; // var_dump prints true
if(($isNotSet == true) || ($moveSuccess == true)){
the script always breaks at exactly that line and doesn't go any further. It won't execute anything after this.
I have tried lots of things, e.g.
if(!isset($moved) || $moved instanceof SuccessNotification){
or
if(!isset($moved) === true || $moved instanceof SuccessNotification){
or
$isNotSet = !isset($moved);
$moveSuccess = $moved instanceof SuccessNotification;
if($isNotSet === true || $moveSuccess === true){
All don't work. I'm aware they all mean the same, I was just trying to eliminate the thought of being on the wrong track. Some of those examples are marked as syntax error in Netbeans, but not all of them, which got me thinking.
I'd like to know what's going on here because this limits coding a lot. But as I said, I probably overlook a totally simple thing.
edit: the script breaks without any errors or warnings shown, it just stops working at that line
edit2:
I took the snippet and executed it on its own.
$moved = new SuccessNotification($code, $title, $message);
echo 1;
$isNotSet = !isset($moved);
echo 2;
$moveSuccess = $moved instanceof SuccessNotification;
echo 3;
if(($isNotSet === true) || ($moveSuccess === true) || 1){
echo 4;
}else{
echo 5;
}
echo 6;
This prints "123", nothing else. And yes, I have included the SuccessNotification class (otherwise there would have been an error anyways) which is being used system-wide already and properly working, just removed that line for posting here.
I take it that nobody really has a clue and therefore tries to find a simple solution which is ok because I thought it's just a simple thing. But I realize I have tried everything you suggest or would do in this situation.

Shouldnt it be:
if(($isNotSet === true) || ($moveSuccess === true)){
Instead of:
if(($isNotSet == true) || ($moveSuccess == true)){

Related

ifelse in echo statement

I have code as bellow :
number_format(($hasilz->harga>100000 ? $hasilz->harga+2000 :
($hasilz->harga>300000 ? $hasilz->harga+4000 :
($hasilz->harga>400000 ? $hasilz->harga+8000 :
$hasilz->harga+10000))), 0, ',', '.')
this code result and read +2000 and +10000 only
any idea ?
Let's see what you got there (simplified):
if (hagara > 100000) {
harga+2000
} else if (hagara > 300000) {
hagara+4000
} else if (hagara > 400000) {
hagara+8000
} else {
hagara+10000
}
If you write it like this, its easy to see. hagara is either >100000 which results in +2000 or it is less, then it results in +10000.
In other words, the two else if will never be true, because if they were, the first if would already be true.
I think this is also a good example, when you should NOT use the tenary operator. It just makes it really hard to read and understand... sometimes the good old it-approach ist just the better solution. ;)
EDIT: To answer the question, you have to use a different order of the if-statements, beginning with the biggest one (or write them completely different). However, as already mentioned, you shouldn't do that.

Weird error when writting OR conditionals in PHP

This error has been bugging me for a long time and I can't find an answer anywhere on the Internet, even using PHP official documentation.
When I write if statements with multiple conditions like this
if ((empty($user) == true) || (isset($user->data) == false)) {
//...
}
PHP says "Call to undefined function ()".
Then I try this alternative:
if (empty($user) == true || isset($user->data) == false) {
//...
}
And PHP says Call to undefined function isset().
PHP version 5.5.15.
By chance I just found the answer to my own problem. I cannot believe it, after all this time.
You're right #Musa
if (empty($user) == true || isset($user->data) == false) {
if (empty($user) == true || isset($user->data) == false) {
I realized something was wrong reconstructing both conditionals and looking at Sublime's syntax highlighting.
I use alt gr to write the pipe symbol and sometimes I left it pressed more than I should and I end up writting alt gr + space. It results in an invisible character that I believed to be a space.
Thanks everyone.
How about:
if (empty($user) || !isset($user->data)) {
//...
}

Stuck Troubleshooting Compound IF Statement

I am experienced programmer, but am not a PHP developer. I have been asked to troubleshoot the following code, but can't see what the problem is. This IF statement works:
<?php
if ($ice_cream_social == 'yes') {
$registration_price = "58.00";
}
?>
However, the page in question requires some compound IF statements to test if form elements have been checked and adjust the price accordingly. This code does NOT work, it doesn't give an error -- but the IF doesn't execute:
<?php
if ($ice_cream_social == 'yes' AND $registration_type == 'Banquet and Conference') {
$registration_price = "78.00";
?>
Using developer tools I have verified the form fields are being passed from the HTML, the code has the variable name and value spelled correctly, and I can test for any ONE variable's value successfully. It seems that when a second or third test is added using the syntax I showed above, the test fails and the IF doesn't run. Based on my limited understanding of PHP's syntax and some Googling, the code looks correct to me (and does not produce any errors), but I am thinking I am missing something about why this isn't working.
Can someone tell me if I'm missing something obvious in my code sample that would cause the IF not to run? I didn't include more code as this is one piece of a messy set of includes :)
Thanks!
It looks like to me on the elseif you don't have a logical check, so you either need to change it to else or add a check(if that is your intention) elseif($registration_price>0)
I used this code to test:
<?php
$registration_price = '';
$ice_cream_social = 'yes';
//$ice_cream_social = 'no';
$registration_type = 'Banquet and Conference';
//$registration_type = 'Something else';
if ($ice_cream_social == 'yes') {
$registration_price = "58.00";
} else {
$registration_price = "not defined";
}
echo $registration_price;
if ($ice_cream_social == 'yes' && $registration_type == 'Banquet and Conference') {
$registration_price = "78.00";
} elseif( 1 > 0) {
$registration_price = "1 million dolars!";
} else {
$registration_price = "not defined";
}
echo $registration_price;

Aptana Simple Inline Conditionals for PHP

I typically use Eclipse as main PHP and JS editor. While the JS Formatting piece does what I want, the PHP Formatting seems completely lacking. Thus, I'd prefer to use Aptana Studio. However, PHP Formatting does at least one annoyance that I'd like to change. The Formatter for Aptana has so many options already, it's surprising that this is not an option.
I prefer for simple inline conditionals to be all on one line, like this:
if ($test == FALSE) return FALSE;
However, Aptana PHP formats like this:
if ($test == FALSE)
return FALSE;
This gets really annoying when you test anything more than 1 at a time:
if ($test1 == FALSE) return FALSE;
if ($test2 == 14) return FALSE;
if ($test3 == "") return FALSE;
becomes:
if ($test1 == FALSE)
return FALSE;
if ($test2 == 14)
return FALSE;
if ($test3 == "")
return FALSE;
I wondered if there was a manual way to force that type of formatting. Any suggestions?
There is no way to force it at the moment. The if body is wrapped as a block and forced to the second line.
What you'll need to do is to ask for an improvement here: http://aptana.com/r/apbugs

How would you go about validating that a regex actually compiles and works, ie doesn't throw an error?

I'm creating an administration panel that has regex's submitted to it. Any ideas on how I would go about validating the submitted regex's to see if they work. When I say "see if they work" I mean if the regex's are valid and compile properly, not that they actually match data or not?
FYI, This would be in PHP.
Solved it myself after checking the docs.
preg_match('/'.$pattern.'/', 'foobar foobar foobar');
if(preg_last_error() === PREG_NO_ERROR)
{
// ok
}
preg_match returns boolean false on error so it's a simple matter of checking the return value (make sure you use the === not ==) and suppress the warning output:
if (#preg_match('/some expression/', '') === false) {
// bad regex
}
Another solution that wont throw a warning but uses ugly error supressing...
$good_re = '~\d+~';
$bad_re = '##$';
$good_check = #preg_match( $good_re, 'asdd' );
var_dump($good_check);
$bad_check = #preg_match( $bad_re, 'asdd' );
var_dump($bad_check);

Categories