Help with simple if then statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Remix919
Forum Newbie
Posts: 4
Joined: Mon Jan 16, 2006 9:37 am

Help with simple if then statement

Post by Remix919 »

Hey guys, I used to know a lot about php code and wrote several games, but haven't used php in quite some years. I was recently building a website and just needed a simple if then statement, here's my code:

Code: Select all

 
<?
if($view == "") {
$view = "main";
}
include "$view.php";
?>
 
The problem is that it'll replace the $view variable and display the main.php easily, but when I use the url like index.php?view=somepage, it won't display it, it won't even show up the $view variable when I echo it. I was just wondering if there's been some major changes to PHP lately to the point where I can't pass a variable through a URL? I have PHP ver 5.2.5.

Thanks in advance for the help! :)
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Help with simple if then statement

Post by watson516 »

Remix919 wrote:Hey guys, I used to know a lot about php code and wrote several games, but haven't used php in quite some years. I was recently building a website and just needed a simple if then statement, here's my code:

Code: Select all

 
<?
if($view == "") {
$view = "main";
}
include "$view.php";
?>
 
The problem is that it'll replace the $view variable and display the main.php easily, but when I use the url like index.php?view=somepage, it won't display it, it won't even show up the $view variable when I echo it. I was just wondering if there's been some major changes to PHP lately to the point where I can't pass a variable through a URL? I have PHP ver 5.2.5.

Thanks in advance for the help! :)

Code: Select all

 
<?php
$view=$_GET['view'];
if($view=="")
{
    $view="main";
}
 
include($view.".php");
?>
 
Something like that I could think.

I believe the ability to reference variables passed via GET with just the variable name is disabled by default in newer version of php. Then again, I could be completely wrong.
Remix919
Forum Newbie
Posts: 4
Joined: Mon Jan 16, 2006 9:37 am

Re: Help with simple if then statement

Post by Remix919 »

Thanks! that code worked perfectly, so apparently now we have to use the GET command now eh, pfft...I don't suppose you know if there's anyway to turn that off so I can get variables directly from the URL without having to do that?
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Help with simple if then statement

Post by watson516 »

Somewhere in your ini file. Not sure where, but its in there.
User avatar
Peter Anselmo
Forum Commoner
Posts: 58
Joined: Wed Feb 27, 2008 7:22 pm

Re: Help with simple if then statement

Post by Peter Anselmo »

The ini setting watson referred to is register_globals

PHP 5 changed it so you must specify _GET by default. This greatly improves the security of your scripts and I (and many other people) would recommend not turning it off, but rather embracing it in your code. Also, you'll soon find it a boon to be able to distinguish GET, POST, SESSION, and COOKIE explicitly. Also, most servers require it now by default, so your code will be much more portable and re-usable if you use it.
Post Reply