-2

I am currently in a process of writing my PHP script, and I am doing it with

E_ALL set in php and I can not allow ANY errors, even

PHP Notice: Undefined variable

And I have many places where i ECHO $myvariable without checking if it's empty, which is causing error mentioned above.

Is it just me or it's extremely stupid to do this for every variable that can be undefined:

if(!empty($myvariable)) {
echo $myvariable;
}

Is this only way to avoid these errors?

EDIT: Question is not a duplicate, what you refereed to as duplicate has nothing to do with what i asked here.

Darktwen
  • 333
  • 5
  • 18
  • than declare as empty at top level `$myvariable = '';` or use `isset()` – devpro Mar 15 '17 at 15:39
  • if not empty does not check if a variable is set – Masivuye Cokile Mar 15 '17 at 15:39
  • @devpro is that "accepted practice" at code canyon? – Darktwen Mar 15 '17 at 15:40
  • and `$myvariable` is where/what exactly? there isn't enough code to support the question, IMHO. Or is this a different question altogether? – Funk Forty Niner Mar 15 '17 at 15:41
  • You could make a `echo_no_notice` function that checks first if it is empty or not...or just not report notices.. `function echo_no_notice($var) { echo (!empty($var)) $var : ''; }` – chris85 Mar 15 '17 at 15:41
  • at best; you could use a ternary operator. If you want to suppress errors, then there are a few ways to go about it. Mind you, I think the question's a tad unclear and probably too broad. – Funk Forty Niner Mar 15 '17 at 15:42
  • @Darktwen Use `isset($myvariable)` or `!empty($myvariable)` . If hiding the error statement is main concern use `ECHO @$myvariable;` – Sahil Gulati Mar 15 '17 at 15:43
  • *"EDIT: Question is not a duplicate, what you refereed to as duplicate has nothing to do with what i asked here."* - You should have pinged @MasivuyeCokile about it. Maybe they left the question? who knows. – Funk Forty Niner Mar 15 '17 at 15:46
  • IMHO, this question could fall under any of these: too broad, unclear, opinion-based. – Funk Forty Niner Mar 15 '17 at 15:48
  • Set dispaly_errors to 0 in your ini file and reboot your web server. Then you can check your error logs for problems. – Jay Blanchard Mar 15 '17 at 15:49
  • IMHO, I really think that there is no such thing as duplicate question. But I understand there are people here on stackoverflow who are just acting like 'guards' and actually never do something helpful to people. Why there is no duplicate question? Because question which is asked on example in 2011 has no same answer in 2017. Maybe there are better solutions in 2017? You never thought that way huh? – Darktwen Mar 15 '17 at 15:50
  • you have 2 answers below (so far), one of which is a duplicate from what was posted in comments. If they didn't solve the question, post a comment under them about it. – Funk Forty Niner Mar 15 '17 at 15:50
  • 1
    @Darktwen There are options on the dups to improve them. Bounty it and select `The current answer(s) are out-of-date and require revision given recent changes.` – chris85 Mar 15 '17 at 15:51
  • 2
    Just because you have that opinion does not make it so. Answers from 2011 could still be valid for 2017. You never thought that way, huh? ¯\\_(ツ)_/¯ – Jay Blanchard Mar 15 '17 at 15:51
  • 1
    @Fred-ii- I arrest my case – Masivuye Cokile Mar 15 '17 at 15:52
  • Although PHP does not require variable declaration, it does recommend it in order to avoid some security vulnerabilities or bugs where one would forget to give a value to a variable that he will use later in the script. – Masivuye Cokile Mar 15 '17 at 15:52
  • in the case of undeclared variables is issue a very low level error, E_NOTICE, one that is not even reported by default.I suggest that you declare your variables,Or use `isset() / !empty()` to check if they are declared before referencing – Masivuye Cokile Mar 15 '17 at 15:55
  • The duplicate shows you how to test for undefined variables and handle them properly. That *is* what you're asking, right? – Jay Blanchard Mar 15 '17 at 15:57
  • One question for the moderator/administrator. Why do they keep duplicate questions on Stack-overflow if they are not helpful? Why they do not just delete them if there is already an answer? – Darktwen Mar 15 '17 at 16:01
  • 1
    *really think that there is no such thing as duplicate question. But I understand there are people here on stackoverflow who are just acting like 'guards' and actually never do something helpful to people* Its people like u @Darktwen that wanna turn SO into another Yahoo answers, we still love this community and we will remain security guards to duplicate questions, I learn't most programing from this commnunity and I refuse to allow people like u to turn it into Yahoo answers – Masivuye Cokile Mar 15 '17 at 16:02
  • 1
    *"One question for the moderator/administrator. Why do they keep duplicate questions on Stack-overflow if they are not helpful? Why they do not just delete them if there is already an answer?"* - That is a question to be posted on meta https://meta.stackoverflow.com/ - However, you're most likely going to see a duplicate for that question also. @Darktwen Or you can flag your own question to a moderator. – Funk Forty Niner Mar 15 '17 at 16:05
  • 1
    Duplicate questions/answers are kept because they provide value to the community @Darktwen It is more effective to have a canonical answer to certain question than it is to have the same question asked and answered multiple time. As Fred -ii- pointed out, if you have a problem with this concept you can ask about it on http://meta.stackoverlfow.com and make sure to include a link to your question here if you do not find a duplicate which is suitable for you there. – Jay Blanchard Mar 15 '17 at 16:07
  • I got downvotes because question is duplicate, and i do not think that is what downvoting option is used for. That is not fair. – Darktwen Mar 15 '17 at 16:09
  • You probably got downvoted because your question showed little or no research. If you want to get the rep back you lost here you can delete the question and you'll get your points back. Keep in mind that downvotes are not personal, only an indication of a question's usefulness. It doesn't matter 'who' did it. ¯\_(ツ)_/¯ You'll have to unaccept the answer in order to delete. – Jay Blanchard Mar 15 '17 at 16:15

2 Answers2

1

Three possible solutions:

1st: initialize your var at the very beginning of your code (or method, or class, whatever you're doing)...

$var = "";

// Do stuff with $var

echo $var;

2nd: use a ternary operator

echo (isset($var)) ? $var : "";

3rd: use @

@echo($var);
napolux
  • 15,574
  • 9
  • 51
  • 70
  • It isn't one variable the OP is trying to fix it for.. – chris85 Mar 15 '17 at 15:45
  • I like 2nd a 3rd solution. @chris85 I am not trying to do anything "bulk" here, I am aware i need to edit every single of my variables and I do not want shortcuts. – Darktwen Mar 15 '17 at 15:47
  • 2
    Error operator isn't a good solution, `isset()` is always better. – Ahmad Mar 15 '17 at 15:49
  • @Ahmad I tried to raise that and OP rejected it – Masivuye Cokile Mar 15 '17 at 15:53
  • I was going to use **2nd** – Darktwen Mar 15 '17 at 15:54
  • 1
    *"I was going to use 2nd"* - To which I said [in this comment](http://stackoverflow.com/questions/42814345/do-i-have-to-check-every-variable-is-set-before-echoing-it#comment72741046_42814345) but you failed to respond to it. @Darktwen btw; it was edited with the ternary before the 5 min. grace period, which is why the revisions history doesn't show. – Funk Forty Niner Mar 15 '17 at 15:55
  • 1
    Option 2 is from the dup, http://stackoverflow.com/a/4261200/4333555. `Or use isset() / !empty() to check if they are declared before referencing them` – chris85 Mar 15 '17 at 15:55
  • Sorry I didn't saw it. If you put answer instead of comment i would accepted it. – Darktwen Mar 15 '17 at 15:56
  • @Darktwen it's ok and thanks for the offer. There's more options in the answer than just a mere ternary. – Funk Forty Niner Mar 15 '17 at 15:57
  • 1
    @Darktwen comments are more important at times than the answer, comments provides extra important information that is not found in most answers – Masivuye Cokile Mar 15 '17 at 15:59
  • @MasivuyeCokile probably because they can't be voted down :D haha – Darktwen Mar 15 '17 at 15:59
1

It is a notice which gives you an insight that your code could produce unexpected results since you did not assign a value (constant or calculated).

You can avoid this error by checking if it's set using the isset function or set a value for that variable at the top of your script.

Peter
  • 6,509
  • 4
  • 30
  • 34
  • They edited this as *"EDIT: Question is not a duplicate, what you refereed to as duplicate has nothing to do with what i asked here."* - Yet they state that the possible duplicate in the question that was placed, said it's not and your answer does exactly that. – Funk Forty Niner Mar 15 '17 at 15:49