php switch am I doing it wrong? - php

I'm trying to make multiple pages in 1 file and i have this
<?
switch($action){
case "add":
add();
break;
default:
hello();
break;
}
function add() {
echo "hello";
}
function hello() {
echo "hello1";
}
?>
but when I got to ****.php?action=add I still get "hello1"
what am I doing wrong with this to where I get hello

switch on $_GET['action'] instead.

<?php
$action= $_GET['action']
//$_REQUEST, by default, contains the contents of $_GET, $_POST and $_COOKIE.
// $action= $_REQUEST['action'];
switch($action){
case "add":
add();
break;
default:
hello();
break;
}
function add() {
echo "hello";
}
function hello() {
echo "hello1";
}
?>

Related

if statement says true to a null variable

What is happening here? Is it to late for my brain?
I have a php file containing exaclty the code below.
<?php
$foo = 'foo';
$bar = null;
switch ($foo) {
default:
case 'foo':
if ($bar) {
echo 'true';
} else {
echo 'false';
}
}
Prints true when run in the browser but false when run on command line... HOW? I noticed when I remove the line default: it works, but how should this whole switch statement be related to this? It's still a simple if (null) { doing this anyway }
PHP 7.0.13
Apache/2.4.18
And yes... I cleared my browser cache, hit ctrl+f5... I even changed the scripts file name.
UPDATE:
After making changes to this simple file... (just adding whitespaces to the end) and hitting f5 in the browser it says false once but than true again... no matter what I do. I really don't get it.
UPDATE:
My PHP version just got updated from 7.0.13 to 7.0.15. Guess what... still the same output:
Apache/Browser: true
Console: false
For this you can use isset
<?php
$foo = 'bar';
$bar = null;
switch ($foo) {
default:
case 'bar':
if (isset($bar)) {
echo "true";
die();
} else {
echo "false";
die();
}
}
This code will echo false.
You could also use a double not operator:
<?php
$foo = 'bar';
$bar = null;
switch ($foo) {
default:
case 'bar':
if (!!$bar) {
echo "true";
die();
} else {
echo "false";
die();
}
}
The problem with your default case
In the code you provided (and the code I re-quoted), you will be hitting case 'bar' every time, no matter what $foo is set to.
<?php
$foo = 'notbar';
$bar = null;
switch ($foo) {
default:
case 'bar':
if (!!$bar) {
echo "true";
die();
} else {
echo "false";
die();
}
}
This code will still echo false.
What you should do is include a break in the default case.
<?php
$foo = 'notbar';
$bar = null;
switch ($foo) {
default:
echo "default";
break;
case 'bar':
if (!!$bar) {
echo "true";
die();
} else {
echo "false";
die();
}
break;
}
This code will echo default and nothing else.

Roll to another case in PHP switch

For example I have this:
switch ($action)
{
case 'my_action':
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
Is the example above going to go through the switch again and then call the first case my_action if the second case second_action has $this_is_true variable set to true?
If this doesn't work, what would be an alternative?
You can try something like this:
switch ($action)
{
case 'my_action':
case 'second_action':
if ($this_is_true || $action=='my_action')
{
doSomething();
}
else
{
doSomethingElse();
}
break;
}
When $action is equal to 'my_action' it will run through the case, as it finds no break sentence then it will run through the second case until it finds the break sentence.
Please give a look to example #3 in http://php.net/manual/en/control-structures.switch.php to find out more about no breaking switch cases.
No it won't. Just call doSomething(); in the 'second_action' case.
I wouldn't use a switch in this simple case, but if it is very long then maybe:
$do = false;
switch ($action)
{
case 'my_action':
$do = true;
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
$do = true;
}
else
{
doSomethingElse();
}
break;
}
if($do) { doSomething(); }
Try to do it this way:
do {
switch ($action)
{
case 'my_action':
$this_is_true = false;
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
} while($this_is_true);
Do not forget to switch $this_is_true to false where it needed!
But this code is not beautiful... May be you better to refactor your code.

Calling PHP functions through another file

I created a simple sample php file for display some function outputs. here is the code..
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('Joel');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('Duck');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
function hello($name){
return $name;
}
function pageHeader($header,$onoff){
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
echo $printHeader;
echo $printName;
?>
This code is working fine without any problems.
When I call 'example.com/php/tipo34.php?red', it shows on the screen:
redpage header
Joel
And when I call 'example.com/php/tipo34.php?blue' it shows on the screen:
Duck
I tried to put the below functions inside of another php file called tipo34-req.php and received the following error:
Fatal error: Call to undefined function pageHeader() in C:\wamp\www\php\tipo34.php on line 8
The code I tried:
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
include 'tipo34-req.php';
echo $printHeader;
echo $printName;
?>
tipo34-req.php code:
<?php
function hello($name){
global $name;
return $name;
}
function pageHeader($header,$onoff){ global $header, $onoff
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
?>
How do I solve this problem? Using the function directly on the file works, but when I put the functions in another php file, it throws the error.
Thanks.
Include your file above its contents usage. PHP is unaware of the functions since they are included later in the code.
include 'tipo34-req.php';
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
echo $printHeader;
echo $printName;
?>
Have you tried including the file at the top before calling any of the functions?

Send value to php file

I'm try to send a value to a PHP file, but when I check, this value became null.
I send the value by: user_login.php?p_action=New_User
The code of user_login.php is:
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
switch ($p_action) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
}
case 'Save_Profile': {
$l_flag = "Save_Profile";
break;
}
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
}
switch ($l_flag) {
case 'New_User': {
include "include/user_new_inc.php";
break;
}
case 'Save_Profile': {
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
}
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}
First your code isn't correct please read more about using Switch here
second to access to any variable came from url you can use Global variable $_GET or $_REQUEST
and you can read more about them from here and here
and this is your code after fixing it please try to run it
<?php
require("include/session_inc.php");
require("include/user_handling_inc.php");
require("include/db_inc.php");
start_Session(false, false);
$p_action=$_GET["p_action"];
switch ($p_action) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
case 'Save_Profile':
$l_flag = "Save_Profile";
break;
case 'New_User':
$l_flag = "New_User";
break;
case 'Create_New_User':
$l_flag = "Create_New_User";
break;
}
switch ($l_flag) {
case 'New_User':
include "include/user_new_inc.php";
break;
case 'Save_Profile':
load_User_Data(" username = '$p_in_username' ", false);
include "include/user_profile_save_inc.php";
break;
case 'Wrong_Password':
echo "Wrong Pass";
break;
case 'OK':
load_User_Data(" username = '$p_in_username' ", true);
store_User_Cookie($g_userdata->user_id);
include "include/user_profile_inc.php";
break;
case 'Create_New_User':
$l_user_id = create_New_User ($p_in_username, $p_in_email, 'Y');
if ($l_user_id != -1) {
store_User_Cookie($l_user_id);
echo "Success !! <br><br> \n";
echo "<a href\"/index.php\"> Back to Main </a>";
}
break;
}
?>
you need to make the code like this friend
switch ($_GET["p_action"]) {
case 'Login': {
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
that well give you the value of the get!!!
Use $_GET to get your parameter.
Sometimes $_REQUEST is preferable since it access both get & post data.
2nd thing never trust the user input so you must use addslashes(); or real_escape_string() function to prevent attacks on the system.
So Code would be like this :
$var = addslashes($_GET['p_action']);
switch($p) {
case 'Login':
$l_flag = verify_User($p_in_username, $p_in_password);
if ($l_flag == "Not_Found") {
$l_flag = "New_User";
}
break;
"OTHER CASES HERE"
}
Notice that : Don't add { } for CASE. Read syntax for switch
here.

PHP Switch statement error

<?php
include 'db.php';
$mail=$_SESSION['session_u_e_mail'];
if(!isset($_GET['edit']))
{
$_GET['edit']=0;
}
switch($_GET['edit'])
{
case 1: echo "Value";
break;
case 2: echo "Value";
break;
default: echo "Value";
break;
}
I m facing problem that the values in every case is echoed twice.
I've just grabbed the
if(!isset($_GET['edit']))
{
$_GET['edit']=0;
}
switch($_GET['edit'])
{
case 1: echo "Value";
break;
case 2: echo "Value";
break;
default: echo "Value";
break;
}
this portion and pasted onto codepad and it works fine.
Here is the working version: http://codepad.org/P8bhL5vl
It outputs just "Value".
There is nothing in this code that would even output your default value. No matter the input, this outputs Value. Look in db.php or the files included by it for debugging echoes.

Categories