I have this code being used on my website
if( isset($_GET['gw'] )){
//this is some content
}
So whenever my url is something like this
http://mywebsite.com/?gw=hello
the content inside that div will appear. It works very well, but now what I want to do is have different content for a specific string. Something like this
if (gw=hello) {
// content here
} else if (gw=goodbye) {
// content here
} else {
//content here
}
I know that is not the code, but does anyone know a way I could do this? Thanks!
Your pretty much there!
if(isset($_GET['gw'])) {
if($_GET['gw'] == "hello") {
}
else if ($_GET['gw'] == "goodbye") {
}
else {
}
}
It's probably best to use a switch statement, so you can easily expand on the conditions in the future.
switch (isset($_GET['gw']) ? $_GET['gw'] : '')
{
case "hello":
// do something here
break;
case "goodbye":
// do something here
break;
default:
// default action if nothing above matches
break;
}
Related
I am in the making of some code that needs to check if a users login details are correct, and I therefore need a lot of if-statements inside each other. If any of the conditions in the if-statements are not true, they should alle return the same value. Is there an easy way of doing this, instead of writing the same multiple times? I have made an example below to visualize my problem. As you can see here I write " else { return false; }" multiple time, and this is what I am wondering if you are able to do more efficiently. Maybe so I only have to write "or else return false" once.
//some code
if (/*some condition*/) {
//some code
if (/*some new condition*/) {
//some code
if (/*some new condition*/) {
//some code
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
I am having a hard time finding a good way to explain my problem, so if you have a more elegant way of explaining it, do not hesitate to edit my post. I am also not quite sure that the title is as good as it could be, so if you have any ideas to an alternativ please say so :)
Lets say you have something like that (I added No):
if ( condition1 ) {
//some code 1
if ( condition2 ) {
//some code 2
if ( condition3 ) {
//some code 3
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
Since each time a condition is false, you exit the function returning false, you can directly test if the condition is false using a negation (if the negated condition is true):
if ( !condition1 ) {
return false;
}
//some code 1
if ( !condition2 ) {
return false;
}
//some code 2
if ( !condition3 ) {
return false;
}
//some code 3
This doesn't reduce the number of if statements, but you avoid many nesting levels and the else statements.
You can also try the switch statement. For many situations it will produce cleaner code.
<?php
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
?>
The switch statement is also compatible with using strings:
<?php
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
?>
Good luck! :)
function settings ()
{
if (!checkRights(2)) {
showRightsInfo();
}
if (checkRights(4)) {
assign("STAMMDATENSTYLE", "");
assign("ADMINSTYLE", "");
}
if (checkRights(3)) {
assign("STAMMDATENSTYLE", "");
("ADMINSTYLE", "display: none;");
} else {
("STAMMDATENSTYLE", "display: none;");
("ADMINSTYLE", "display: none;");
}
("PAGETITLE", "Einstellungen");
("MAINPAGE", "a_settings");
("settings");
}
So i have this code function settings, which is executed why i want to see the settings in my program. My available rights are 1,2,3,4: where 4 should show everything and 3 only "stammdatenstyle".
My function checkRights() returns me if the rights of the current logged in user are higher than the requested rights. I am desperately trying to change my code because I only added a 4th. rights group which can see everything and before "group" 3 was able to see everything. Where is the problem? Do i have an error in my settings()?
<div class="panel panel-default" style="{STAMMDATENSTYLE}">
(....)
<div class="panel panel-default" style="{ADMINSTYLE}">
(....)
The problem is that your falling through the various if conditions and the last if says if = 3 then do this else do this. So condition 4 will get overwritten by this last set of settings.
The way I would do it is to set the default values to start with and then adjust the ones you want to...
function settings ()
{
if (!$this->ctb->checkRights(2)) {
$this->ctb->showRightsInfo();
}
$this->ctb->tpl->assign("STAMMDATENSTYLE", "display: none;");
$this->ctb->tpl->assign("ADMINSTYLE", "display: none;");
if ($this->ctb->checkRights(4)) {
$this->ctb->tpl->assign("STAMMDATENSTYLE", "");
$this->ctb->tpl->assign("ADMINSTYLE", "");
}
if ($this->ctb->checkRights(3)) {
$this->ctb->tpl->assign("STAMMDATENSTYLE", "");
}
$this->ctb->tpl->assign("PAGETITLE", "Einstellungen");
$this->ctb->tpl->parse("AUSSCHUSSMAINPAGE", "ausschuss_settings");
$this->buildPage("settings");
}
You could also change to use if() .. elseif... else... but make sure you get the right combinations.
you have the following if/else construct:
if(condition){
// something
}
if(condition){
// something
}else{
// something
}
this will cause problems surely. as you can see, if the first if-condition is met and the second is not met it will execute two parts of the script, instead of one.
Example:
$color = 'blue';
if($color == 'blue'){
echo 1;
}
if($Color == 'green'){
echo 2;
}else{
echo 3;
}
This will output 1 and 3 and this is how your current script works.
Therefore you should use at least something like elseif
if(condition){
// something
}elseif(condition){
// something
}else{
// something
}
but I recommend you to use switch
switch(condition){
case 1:
// something;
break;
case 2:
// something;
break;
case 3:
// something;
break;
case 4:
// something;
break;
default:
// something
}
If I want to create an if statement with 2 variables:
if ($variable1 && $variable2) {
// Do something
}
And then add another if statement below with only the first variable, how would I do it? Do I only include the one variable like this:
else if ($variable1) {
// Do something
}
Or do I need to specify that the first variable is true, not the second? If so, is this correct?
if ($variable1 && !$variable2) {
// Do something
}
Go for:
if ($variable1 && $variable2) {
// Do something
}
else if ($variable1) {
// Do something
}
the reason is for example if you write like this :
the following is wrong approach
if ($variable1) {
// if u have two variables $variable1 and $variable2
// and you want to validate both but if the $variable1 contains
// nonzero value it will never go to the else part
}
else if ($variable1 && $variable2) {
// Do something
}
now basically
else if ($variable1) {
// Do something
}
and
else if ($variable1 && !$variable2) {
// Do something
}
are same.you can use any of them if you are not toooo much concerned about the performance.
else if ($variable1) {
// Do something
}
is enough. Since the first if-statement will fail, it will evaluate the else if as a new statement
I've got 3 variables:
$left_side = "'Username'";
$equation = "==";
$right_side = "'Username'";
I want to test these variables as it was an if statement like so:
if($left_side $equation $right_side) {
// do something
} else {
// do something else
}
I know this works:
if(eval("return ".$left_side." ".$equation." ".$right_side.";")) {
// do something
} else {
// do something else
}
I always tought it's 'not good' to use eval. Especially when you try to run user input.
Is there an other way to do this? I tried to google it, but it's not my friend to day ;)
You may do something like this:
function testValues($val1, $equation, $val2) {
$res = false;
switch($equation) {
case "==":
$res = $val1 == $val2;
break;
case ">":
$res = $val1 > $val2;
break;
//....
default:
throw new Exception("Unknown operator");
}
return $res;
}
and than use it like:
if(testValues($left_side,$equation,$right_side)) {
//do something
} else {
//do something
}
eval is evil. And no, there's no other (easy) solution, but maybe this one helps:
if ($equation == "==") {
if ($left_side == $right_side) {
// ... your code goes here.
} else {
// Do some other stuff.
}
}
You could use switch:
$left_side = "'Username'";
$equation = "doublequal";
$right_side = "'Username'";
switch($equation){
case 'doublequal':
if ($left_side == $right_side) {
// code
}
break;
//......
}
You should never use eval() especially with user input.
eval() is evil, but call_user_func() is evil too and every framework uses this function in one place or another.
Tools aren't evil. They are just tools. Evil is the way that we use them.
As Uncle Ben said: Great power involves great responsibility :)
I find this trick
http://gonzalo123.com/2012/03/12/how-to-use-eval-without-using-eval-in-php/
the idea is create a temporary file with the PHP source code, include this file with the standard PHP’s include functions and destroy the temporary file.
I have a bit of a problem with my PHP code, I am assigning values to variables in different states of the flow depending on what I receive, but for some reason it keeps getting stuck at one point, here is the code.
if (isset($session)) {
//if the user is in the database
if ($row == 1) {
$from = $_GET['from'];
if (isset($from)) {
$page = $_GET['page'];
switch ($page) {
case "game":
$page = "game";
sendVars($page);//send the variable
break;
case "gallery":
$page = "gallery";
sendVars($page);//send the variable
break;
case "leaderboard":
$page = "leaderboard";
sendVars($page);//send the Variable
break;
}
}else {
$page = "game";
sendVars($page);//send the variable
}
//if the user is not in the database
}else {
//do this
}
} else {
//register
}
Now for some odd reason, it keeps setting the value of $page to game, even though I set the page variable to gallery like so http://www.mydomai.com/?from=set&page=gallery . the only reason for this that I can think of is that my switch is not working as it should? or it is bypassing the switch somehow?
Thanx in advance!
I just ran your code after removing a few of the unessersary variable assignments:
<?php
// I added this function just for testing
function sendVars($page) {
echo $page;
}
if (isset($_GET['from'])) {
$page = $_GET['page'];
switch ($page) {
case "game":
sendVars($page); //send the variable
break;
case "gallery":
sendVars($page); //send the variable
break;
case "leaderboard":
sendVars($page); //send the Variable
break;
}
} else {
$page = "game";
sendVars($page); //send the variable
}
And it all seems fine, xxx.php?from=1&page=gallery echos out "gallery", try doing a print_r($_GET) at the top of your script and see what it prints out and let us know.
On a side note I think the below may be shorter for you and still do the same thing:
if (isset($_GET['from'])) {
// Check if $_GET['page'] exsists and is either game, gallery or leaderboard
if (isset($_GET['page']) && in_array($_GET['page'], array('game', 'gallery', 'leaderboard')))
sendVars($_GET['page']);
}
else
sendVars('game');
I hope this helps
Cheers
Luke
Try doing a var_dump($page);exit; before the switch and see what it spits out.
Also you can do a var_dump($from) and see what that is spitting out - it may be that it goes to the else, so it may not even be getting to the switch.
If this is inside a function, I personally prefer guard-style clauses than constantly increasing the levels of indentation. The idea is you pick out the bad conditions (ie if something is going wrong) to "protect" the larger block of logic.
In your case that's the switch statement.
if (!isset($session))
return ...; // register
if ($row != 1)
return ...; // do this
$from = $_GET['from'];
$page = $_GET['page'];
if (isset($from)) switch ($page) {
case "game":
$page = "game";
break;
case "gallery":
$page = "gallery";
break;
case "leaderboard":
$page = "leaderboard";
break;
}
else $page = "game";
return sendVars($page);// don't need to repeat this if all cases do it!
It's just a style of code and it's not going to fix all (if any) of your problems. You actually don't need the switch block in there for this code. I can't see that it's doing anything.
You don't necessarily need the switch statement. I can't see an obvious problem with your logic but I think this will do the same thing:
if (isset($session))
{
//if the user is in the database
if ($row == 1)
{
$page = (in_array($_GET['page'],array('game','gallery','leaderboard'))) ? $_GET['page'] : "game";
sendVars($page); //send the variable
}
else //if the user is not in the database
{
//do this
}
}
else
{
//register
}
Just saw Luke used the same in_array method but 25 mins before me. Serves me right for being slow!