Weird error when writting OR conditionals in PHP - 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)) {
//...
}

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.

IF with multiple conditions not working

I am trying to create an if statement with multiple conditions but it won't work and perhaps it is my syntax.
I have two post variables, which both give me the value fine elsewhere on my page. They are:
$_POST['text']
$_POST['rating'] //can be G, PG or R
What I am trying to do is make my word filter code work only if the rating equals "G"
What is currently happening though is that the filter is flagging a bad word regardless of the rating and ignoring my IF rating = G part.
if (isset($_POST['text']) and $_POST['rating'] = "G") {
//give warning if bad words are used
}
<?php
if (isset($_POST['text']) && $_POST['rating'] =="G") {
//give warning if bad words are used
}?>
use it like this
You may also use the symbol version of the syntax.
&& for and
|| for or
Also, = does not mean equals in an if statement. That is the syntax for setting a variable.
You would say == (equals to).
To say "not equals" you would use != or !=
if (isset($_POST['text']) && $_POST['rating'] == "G") {
//give warning if bad words are used
}

php - issue with booleans in if statement

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)){

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