Log in

View Full Version : PHP Switch - mini tutorial.



Excellent2
11-09-2008, 10:12 PM
Hey, just wanted t share a cool little function I once came upon a while back when I was new to PHP. The name itself is pretty self explanatory allowing for you to easily switch through pages, so lets start.

Start off with a base code like this:

<?php

switch($_GET[page]) {
default:
echo "<a href='?page=hello'>Hello</a>";
break;

case 'hello':
echo "Hello";
break;
}

?>Now let me explain what this does, first of all we initiate the switch function then use the $_GET statement to grab the page we want to view.

default: is basically the default page upon page load, so if you went to www.site.com/index.php (http://www.site.com/index.php) you'd get a link with "Hello" on it. We then use the break; statement to break the page.

Case.
We use the case statement to grab the end of the url from the default echo which in our case was hello, this allows us to link to an entirely different page using that simple feature.

Have fun!
Oh, you can also do multiple clauses by using:


<?php

switch($_GET[page]) {
default:
echo "<a href='?page=hi'>Hi</a>";
break;

case 'hi':
echo "<a href='?page=bonjour'>Bonjour</a>";
break;

case 'bonjour':
echo "Bonjour!";
break;
}

?>See what I did?

Closed by Invent (Forum Moderator): Thread closed due to prevent (further) arguments.

Jme
11-09-2008, 10:15 PM
You need to add slashes before your quotes so it doesn't think you're ending the string.



<?php

switch($_GET[page]) {
default:
echo "<a href\"=?page=hi\">Hi</a>";
break;

case 'hi':
echo "<a href=\"?page=bonjour\">Bonjour</a>";
break;

case 'bonjour':
echo "Bonjour!";
break;
}

?>
Also, you missed a quote in your first <a> tag :P

Nevermind. I see you've edited it now anyway :)

Protege
11-09-2008, 10:19 PM
Delete, forum lagg.

Protege
11-09-2008, 10:19 PM
I think php.net would help them better tbh. Seeing as you can't even get the alignment right in a switch, making it confusing for even me so even more confusing for a beginner.

I don't think you needed to post this. Sorry.

$_GET[ 'page' ] or $_GET[ "page" ]

& default is suppose to be at the bottom.

@ Invent, support untidy coding +rep

Invent
11-09-2008, 10:21 PM
Seeing as you can't even get the alignment right in a switchWhat do you mean by that? Indentation? If so, indenting code is a personal preference and you don't have to do it.

So you think you should have to indent code which is a personal preference? Then why the hell do you and other people have a go at charlie when he says things about using parenthesis with language constructs when you're doing the exact same thing moaning about indentation?

Excellent2
11-09-2008, 10:25 PM
You need to add slashes before your quotes so it doesn't think you're ending the string.



<?php

switch($_GET[page]) {
default:
echo "<a href\"=?page=hi\">Hi</a>";
break;

case 'hi':
echo "<a href=\"?page=bonjour\">Bonjour</a>";
break;

case 'bonjour':
echo "Bonjour!";
break;
}

?>
Also, you missed a quote in your first <a> tag :P

Nevermind. I see you've edited it now anyway :)
Yeah the proper way would be for slashes but for mini things like this tutorial for beginners I just use '' :P Yeah I edited haha, saw the rest of the code was red.


I think php.net would help them better tbh. Seeing as you can't even get the alignment right in a switch, making it confusing for even me so even more confusing for a beginner.

I don't think you needed to post this. Sorry.
The alignment is fine and indentation is not something I always use..


What do you mean by that? Indentation? If so, indenting code is a personal preference and you don't have to do it.Yeah he's on about indentation which as you said is a personal preference, he's just being picky to my work.

Dentafrice
11-09-2008, 10:28 PM
It should be indented though for a tutorial, as you're teaching people who do not know how to code.. incorrect procedures..

They won't learn to indent their code.

Plus default: should be at the bottom.

Protege
11-09-2008, 10:29 PM
+rep Excellent2

Nice work

+rep to Invent as I need to spread.

Excellent2
11-09-2008, 10:36 PM
It should be indented though for a tutorial, as you're teaching people who do not know how to code.. incorrect procedures..

They won't learn to indent their code.

Plus default: should be at the bottom.I'll remember that for my next tutorial :)

Hypertext
11-09-2008, 10:59 PM
switch isn't a function.

:rolleyes:

If your going to make a tutorial, at least code it well, indentation, having default at the bottom, using strings in your get array.

oh denta just said the exact same thing... thats like a teacher teaching you to code in tables -- you shouldn't do it.

Excellent2
11-09-2008, 11:03 PM
switch isn't a function.

:rolleyes:

If your going to make a tutorial, at least code it well, indentation, having default at the bottom, using strings in your get array.

oh denta just said the exact same thing... thats like a teacher teaching you to code in tables -- you shouldn't do it.Statement then. default can go anywhere it wants, I prefer it at the top because it's much more easier to organize my cases:rolleyes:

madchild24
11-09-2008, 11:28 PM
I'm pretty sure theres an easier way to do this same thing.. with less code. but i maybe wrong.. **looks on computer for it**

Protege
11-09-2008, 11:30 PM
(23:20) James -> Nicole: $_GET[page]
(23:20) James -> Nicole: what a ...
(23:21) James -> Nicole: wops
(23:21) James -> Nicole: i usually look at the bottom 4 the default
(23:21) TehUpload: same
(23:21) TehUpload: that's where it goes
(23:21) TehUpload: because it says
(23:21) TehUpload: if it is this, bla.
if it is this, bla.
if it isn't either one, bla (default)
(23:21) TehUpload: not
(23:22) TehUpload: if it is neither one, bla (default)
if it is this, bla.
convo with Celeb and I, talking about this.

Dentafrice
11-09-2008, 11:31 PM
switch($_GET ["type"]) {
case "about" :
$core->template->parseTemplate( "about" );
break;

case "contact" :
$core->template->parseTemplate( "contact" );

default :
$core->template->parseTemplate( "index" );
break;
}

Agnostic Bear
11-09-2008, 11:42 PM
switch($_GET ["type"]) {
case "about" :
$core->template->parseTemplate( "about" );
break;

case "contact" :
$core->template->parseTemplate( "contact" );

default :
$core->template->parseTemplate( "index" );
break;
}

That would parse 2 templates when you use contact.

Dentafrice
11-09-2008, 11:43 PM
oops forgot that break.



switch($_GET ["type"]) {
case "about" :
$core->template->parseTemplate( "about" );
break;

case "contact" :
$core->template->parseTemplate( "contact" );
break;

default :
$core->template->parseTemplate( "index" );
break;
}

Agnostic Bear
11-09-2008, 11:46 PM
Obviously as pointed out above you can drop-through cases, e.g:


switch( $_GET['act'] )
{
case 'lol':
case 'what':
case 'thing':
echo('lol');
break;

default:
echo('default');
break;
}

case "lol" would drop through what and thing and echo lol, so would what, and also thing.

Dentafrice
11-09-2008, 11:47 PM
Pointing out above, expanding what Dan said, you can use default and index as the same way, and drop-through the cases.

Hypertext
11-09-2008, 11:56 PM
Caleb correct me if I'm wrong, isn't it bad to have spacing like

$_GET ["var"]

Between the T and square left bracket? I don't know...

I think we should try and conform to pear.php.net's standards, they are quite good. I'm sick of everybody adding whitespace and parenthesis EVERYWHERE to make it look 'pretty'..

Whitespace and unecessary characters should only be used where it makes it easier to read, and less to type, IMO.

Dentafrice
12-09-2008, 12:00 AM
I don't type the spaces, I don't type any of the formatting.

I have a nice little too called Ctrl+Shift+F which does it all for me, to phpDoc standards.. which are highly acceptable ;)

pear's standards are laughed at, and are only used for pear repository coding, not a standard for coding.

Excellent2
12-09-2008, 12:00 AM
Caleb correct me if I'm wrong, isn't it bad to have spacing like

$_GET ["var"]

Between the T and square left bracket? I don't know...

I think we should try and conform to pear.php.net's standards, they are quite good. I'm sick of everybody adding whitespace and parenthesis EVERYWHERE to make it look 'pretty'..

Whitespace and unecessary characters should only be used where it makes it easier to read, and less to type, IMO.And I'm sick of your attitude. You have to understand Charlie that people have coding preferences, just because they may not be the same of yours there is no need to complain?

Dentafrice
12-09-2008, 12:02 AM
No, he's just acting like he knows something.. and is trying to "outsmart me" and tell me the "correct" way to do things.

Yet at the beginning of this year, had had a Habbo JPEG avatar, didn't know a lick of PHP, and liked to "test divs".

Excellent2
12-09-2008, 12:09 AM
I just want to say something to Charlie.

Just because someone uses

echo "hi";Or someone else uses

echo 'hi';Or another person uses

echo ("hi");It still works, there isn't any need to bang on about parenthesis or pretty code, you need to learn to respect that people have very different methods of coding and people always will. I would understand if the methods didn't work or absolutely killed loading times but they don't.

@denta, lol.

Dentafrice
12-09-2008, 12:11 AM
Well there really is no point in doing:



echo 'hello ' . $name . ' yeah so here is the motd: ' . $motd . ' cause today is ' . $date . ' yeah so k.';


when you could just do:



echo "hello {$name} yeah so here is the motd: {$motd} cause today is {$date}";

Excellent2
12-09-2008, 12:13 AM
Well there really is no point in doing:



echo 'hello ' . $name . ' yeah so here is the motd: ' . $motd . ' cause today is ' . $date . ' yeah so k.';
when you could just do:



echo "hello {$name} yeah so here is the motd: {$motd} cause today is {$date}";

Yeah I know but there is some strange coders out there that would probably prefer that :P Everyone needs to respect it though.

Dentafrice
12-09-2008, 12:41 AM
Not really, if you do it the way like I said.. it's wasting time.. and giving PHP to hard of a time.

'' was really invented for short phrases and short strings with no variables. Hence the reason in C languages it's max char range is 3.

Hypertext
12-09-2008, 01:21 AM
Well there really is no point in doing:



echo 'hello ' . $name . ' yeah so here is the motd: ' . $motd . ' cause today is ' . $date . ' yeah so k.';
when you could just do:



echo "hello {$name} yeah so here is the motd: {$motd} cause today is {$date}";

I feel like being an *******:

Well there really is no point in doing:



echo "hello {$name} yeah so here is the motd: {$motd} cause today is {$date}";
when you could just do:



echo "hello $name yeah so here is the motd: $motd cause today is $date";


More to the point coding preference can be bad. Using parenthesis or wrong quotes in any script can and has been proven and has legitimate reasons to slow it down.

Dentafrice, is that built into ZDE?

Protege
12-09-2008, 02:20 AM
and I do this

echo( 'I rather do this, cause ' . $im_awesome . ' and thats that.' );

OMG PARENTHESIS' OMG MURDERR REIGGNZ JA RULLE my neg

Tomm
12-09-2008, 07:26 AM
phpDoc is a documenter for PHP. I don't see what this has to do with code formatting?


I don't type the spaces, I don't type any of the formatting.

I have a nice little too called Ctrl+Shift+F which does it all for me, to phpDoc standards.. which are highly acceptable ;)

pear's standards are laughed at, and are only used for pear repository coding, not a standard for coding.

Agnostic Bear
12-09-2008, 07:29 AM
phpDoc is a documenter for PHP. I don't see what this has to do with code formatting?

Precisely, I've just been looking through this thread trying to come up with something to say which will shut everyone up.

Coding PHP has a lot of preference mixed into it, whether you want to use () or not, " or ', they all have their uses and it's all preference. There is no right or wrong, nor is there with indenting, so stop bloody going on about it. The only benefit with using " against ' and () against none is the speed would be increased if you were coding on a server with heavy traffic. Which none of you in this thread do. (Unless Invent has posted).

RYANNNNN
12-09-2008, 08:29 PM
Precisely, I've just been looking through this thread trying to come up with something to say which will shut everyone up.

Coding PHP has a lot of preference mixed into it, whether you want to use () or not, " or ', they all have their uses and it's all preference. There is no right or wrong, nor is there with indenting, so stop bloody going on about it. The only benefit with using " against ' and () against none is the speed would be increased if you were coding on a server with heavy traffic. Which none of you in this thread do. (Unless Invent has posted).

i agree with your post about coding preference

habboxforum webdev section really is a joke now with everyone trying to outsmart other people, if people dont have anything positive or constructive to say then they should post at all

Hypertext
12-09-2008, 09:35 PM
Precisely, I've just been looking through this thread trying to come up with something to say which will shut everyone up.

Coding PHP has a lot of preference mixed into it, whether you want to use () or not, " or ', they all have their uses and it's all preference. There is no right or wrong, nor is there with indenting, so stop bloody going on about it. The only benefit with using " against ' and () against none is the speed would be increased if you were coding on a server with heavy traffic. Which none of you in this thread do. (Unless Invent has posted).

Precisely, but should that be OK? Should people use proven bad code, to make it look better, should it really be a preference if it is bad?

I don't think so.

@caleb: I'm still looking for phpDoc standards, as far as I see they don't have any, googling produced no obvious results.

Excellent2
12-09-2008, 09:42 PM
Precisely, but should that be OK? Should people use proven bad code, to make it look better, should it really be a preference if it is bad?

I don't think so.

@caleb: I'm still looking for phpDoc standards, as far as I see they don't have any, googling produced no obvious results.Listen, just because you're probably following the advice of a few PHP coders doesn't mean it's right. The war in iraq some believe is right, doesn't mean it is - you should just learn to respect peoples preferences over your own.

Want to hide these adverts? Register an account for free!