...

View Full Version : ?action= help?



AJM
11-25-2011, 04:41 PM
Hi all,

I am fairly new PHP (about a years experience). I was just wondering if someone could explain to me how I would be able to make, for example, index.php?action=post show different code than index.php.


Thanks,

-AJM

Fou-Lu
11-25-2011, 04:47 PM
That would be your querystring. Anything provided in it can be retrieved from the $_GET superglobal.


$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action)
{
case 'post':
print 'Action "post" provided';
break;
default:
print 'No valid action provided.';
}

For example.

AJM
11-25-2011, 04:51 PM
Many thanks to you Fou-Lu. Hopefully I can implement this into my pages.

Thanks again,

-AJM

myfayt
11-25-2011, 07:13 PM
There is a few methods to use, here is the one I use.


if (isset($_GET["x"])) {
$x = explode(":",$_GET["x"]);

switch($x[0])
{
case 'deposit':
deposit();
break;

case 'withdraw':
withdraw();
break;


}
}
else {
bank();
}

So if you do bank.php it automatically loads the bank() function.
or if you want something else it'd be like bank?x=deposit
If you don't want to auto load a function, remove the else statement.

Fou-Lu
11-25-2011, 07:40 PM
You can skip the if/else completely. If you default the assignment to a non-viable option such as an empty string, you can use the switch and provide it with the default case which executes when no other options are available to use.

AJM
11-25-2011, 10:05 PM
I am using cookies with this for an admin manage page/script so I AM using if statements. My code looks a bit like this:
if(isset($_COOKIE['cookie']) && $_GET['id'] == "1")
print "Page Stuff";
else
print "";

devinmaking
11-25-2011, 11:48 PM
I am using cookies with this for an admin manage page/script so I AM using if statements. My code looks a bit like this:
if(isset($_COOKIE['cookie']) && $_GET['id'] == "1")
print "Page Stuff";
else
print "";



I might be wrong and i have only been doing this for a few months but i was always told that print ""; is actually slower than echo "";

So to optimise the speed i would use echo and not print

AJM
11-26-2011, 12:34 AM
I might be wrong and i have only been doing this for a few months but i was always told that print ""; is actually slower than echo "";

So to optimise the speed i would use echo and not print

Thanks for the advice man... I'll try it out.

-AJM

Fou-Lu
11-26-2011, 04:51 AM
I might be wrong and i have only been doing this for a few months but i was always told that print ""; is actually slower than echo "";

So to optimise the speed i would use echo and not print

This is a loaded benchmark that is difficult to determine. The variable evaluation does tend to be faster in echo while using a concat instead of internal expansion. Printf with evaluation I'd expect to beat both of these.
I can manufacture a benchmark which supports probably +/- upwards of 200% or so off of each, but one thing to mention is that we are talking 2 or 3 nano-seconds difference. That's peanuts. You will find that print will more consistently beat echo in constant data, while echo tends to beat print in concatenated data (which is why I'd expect printf to defeat both, though I'm not sure how that would related with constant data [which given its origin will likely beat both in both constant and evaluated data]).
For what you'd save, I'd not recommend switching between the two. I'd suggest choosing one and sticking with it for consistency.

devinmaking
11-27-2011, 09:03 AM
This is a loaded benchmark that is difficult to determine. The variable evaluation does tend to be faster in echo while using a concat instead of internal expansion. Printf with evaluation I'd expect to beat both of these.
I can manufacture a benchmark which supports probably +/- upwards of 200% or so off of each, but one thing to mention is that we are talking 2 or 3 nano-seconds difference. That's peanuts. You will find that print will more consistently beat echo in constant data, while echo tends to beat print in concatenated data (which is why I'd expect printf to defeat both, though I'm not sure how that would related with constant data [which given its origin will likely beat both in both constant and evaluated data]).
For what you'd save, I'd not recommend switching between the two. I'd suggest choosing one and sticking with it for consistency.

I did not know this, thanks for the heads up :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum