If statement if variable for string does not exist...?

Go To StackoverFlow.com

-1

how would I write an IF statement that says if $req does not blank and equals string that does not exist then load, load_template(tpl_path.'err404.tpl'); so if $req does not match or equal something that is valid then show error page.

if ($req == '')              load_template(tpl_path.'index.tpl');
if ($req == 'dologin')      include 'includes/dologin.php';
2012-04-04 05:47
by acctman
What is a string that doesn't exist for you? That sounds more philosophical than an actual problem statement to me - Joey 2012-04-04 05:50
?? maybe you are asking for isset? if (!isset($req) || $req == '') { ... } elseif ($req == 'do') { ... } - ShinTakezou 2012-04-04 05:52
How will $req be equal to string that does not exists - Starx 2012-04-04 05:56
Show some dry case - Starx 2012-04-04 05:57
I believe what you're looking for is elseif and else - powerbuoy 2012-04-04 06:02


2

Use a switch statement:

switch($req) {
  case '':
    load_template(tpl_path.'index.tpl');
    break;
  case 'dologin':
    include 'includes/dologin.php';
    break;
  default:
    load_template(tpl_path.'err404.tpl');
}
2012-04-04 05:51
by Emil Vikström
How does it check does not exist part - Starx 2012-04-04 05:52
isset()-function @ Star - peipst9lker 2012-04-04 05:52
@peipst9lker, And where is that on the answer - Starx 2012-04-04 05:53
Starx, I assume that "does not exist" mean something like "if it doesn't match anything else". The question never talks about that $req itself doesn't exist, just that its value is a non-blank string that "does not exist" - Emil Vikström 2012-04-04 05:54
@peipst9lker and Starx I think acctman is just asking us to tell him how to use simple else in if else or default in switch case... so the answer is perfect for him - noob 2012-04-04 05:56
@micha, I am bad at guessing problem - Starx 2012-04-04 05:58
$req is just a variable that pulls the correct template file. ex: site.com/?req=dologin in which will display the login page. if a user type site.com/?req=dologin123 nothing will show because that template does not exist. in cases like that I want the err404.tpl to be displayed - acctman 2012-04-04 05:59
You could switch to switch :) or you could simply change your if statements to elseifs (and a final else displaying the 404): if (nothing) default-page elseif (something) some-page elseif (anotherthing) another-page else 404-pagepowerbuoy 2012-04-04 06:02
@peipst9lker there would be a lot of else statements if I did that, I thought there would be a short one line way of doing it. i'll try the switch statemen - acctman 2012-04-04 06:03
if you just insert if (!isset($req)) { load index... } above the switch youre fin - peipst9lker 2012-04-04 06:04
I guess that they mean something like »Take $req and look if it matches a .tpl file in the file system. If so, then include it, otherwise throw 404.« Hardcoding the possible choices is easier than solving the possible security problems though, at least for a small number of choices. EDIT Oh, nevermind, they said that already. But didn't update the question - Joey 2012-04-04 07:52
Ads