• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

php if statement help

As long as you haven't had any output on that page, try

PHP:
if ($forumid == 11)
{
header("Location: http://www.example.com");
}
 
Code:
if ($forumid == 11)
{
header("Location: http://www.example.com");
}

The above is your code, should it be this below? (Semi colon at the end of line 1)

Code:
if ($forumid == 11);
{
header("Location: http://www.example.com");
}
 
themoose said:
lol, you like picking holes in Tree NC's code, dont you? :D :p
No, i was pointing it out so that the thread starter does not have bad code on there site which will cause errors.
 
robert allen said:
Code:
if ($forumid == 11)
{
header("Location: http://www.example.com");
}

The above is your code, should it be this below? (Semi colon at the end of line 1)

Code:
if ($forumid == 11);
{
header("Location: http://www.example.com");
}
No, it shouldn't. You don't have a semi-colon with functions that have {} like while, if, else etc. Only what's inside the {}
 
Putting a semi-colon after the if statement would return a parse error. On some more-lenient servers, it might just terminate the if statement.
 
And keep in mind you can only use the "header" function to redirect if there has been no HTML output yet.
 
bobby2guns2003 said:
Hey, I would I make a php if statement that would do this:

if $forumid = 11 then redirect to www.example.com

maybe something like this could work:

PHP:
<?php
/* predefined list of available url redirects - could also be array from database*/
$urls=array();
$urls[1]="http://www.google.com";
$urls[2]="http://www.freewebspace.net";
$urls[3]="http://www.deluxnetwork.com";

if(isset($id)){
	$url=$urls[$id];
	if($url==""){
		header("Location: http://www.default.com/");
	}
	else{
		header("Location: $urls[$id]");
	}
}
?>
 
robert allen said:
Code:
if ($forumid == 11)
{
header("Location: http://www.example.com");
}

The above is your code, should it be this below? (Semi colon at the end of line 1)

Code:
if ($forumid == 11);
{
header("Location: http://www.example.com");
}
Wow. You know absolutely nothing about PHP syntax, do you?
 
I don't think that was necessary. One of the most basic rules of PHP is to have a semi-colon at the end of things, with some exceptions. If he just started learning, he might've not known that.
 
if ($forumid == 11)
{
header("Location: http://www.example.com");
}

is OK but if you see some HTML headers ERROR just add this line on start of the page before any code and html.

ob_start();
 
Back
Top