Reputation: 19
I have the following code:
switch ($page)
{
default:
$page = 'error';
// header
require_once 'pages/header.php';
// content
require_once 'pages/error.php';
break;
case 'index':
case 'login':
case 'register':
case 'profile':
// header
require_once 'pages/header.php';
// content
if (file_exists('pages/' . $page . '.php')) require_once 'pages/' . $page . '.php';
break;
}
/*
* Footer
*/
require_once 'pages/footer.php';
Now let's take an example, and apply the following code to header.php:
if ($page == 'profile'):
include 'members/core/init.php';
if(isset($_GET['username']) && empty($_GET['username']) === false) {
$username = htmlentities($_GET['username']);
if ($users->user_exists($username) === false) {
header('Location:index.php');
die();
}else{
$profile_data = array();
$user_id = $users->fetch_info('id', 'username', $username);
$profile_data = $users->userdata($user_id);
}
endif;
And the following, to footer.php:
if ($page == 'profile'):
}else{
header('Location: index.php');
}
endif;
Site structure it's following:
index.php
pages (Folder)
=
header.php
index.php
footer.php
profile.php
But the problem its header.php and footer.php , wich seems to not make connection one with another, cuz i receive the following error:
Parse syntax error, T_ENDIF
How can i change code to make connection for header.php with footer.php ?
Cheers!
Upvotes: 0
Views: 473
Reputation: 11047
Put your default statement after all specific case statement. Other wise the default statement block will be executed and other cases ignored
switch ($page)
{
case 'index':
case 'login':
case 'register':
case 'profile':
// header
require_once 'pages/header.php';
// content
if (file_exists('pages/' . $page . '.php')) require_once 'pages/' . $page . '.php';
break;
default:
$page = 'error';
// header
require_once 'pages/header.php';
// content
require_once 'pages/error.php';
break;
}
Edit:
There is no endif;
statement in php
Sorry, I learned somthing... Thanks :)
Upvotes: 0
Reputation: 97718
As others have pointed out, you are mixing if() { ... }
and if(): endif;
, which is confusing things. There's no actual difference between the two, but they have to match up in pairs - you can't write if ( test_something() ) { do_something(); endif;
This is documented in the manual under Alternative syntax for control structures:
Note: Mixing syntaxes in the same control block is not supported.
The other thing to note is that each PHP file included must have valid syntax on its own - PHP doesn't stick all the code together and then parse it - so you can't open an if
statement in one file and then close it in another. (I can't actually find a good manual reference for this; if someone knows one, let me know in the comments, or edit it in here.)
If you always indent further when you open an if
statement or similar block, the structure of code generally becomes much clearer. If we do this to your code, and strip out everything else, we are left with this; see the comments I've added for where it's going wrong:
// header.php
if ($page == 'profile'):
if(isset($_GET['username']) && empty($_GET['username']) === false) {
if ($users->user_exists($username) === false) {
}else{
}
// Closing with endif, but opened with {
endif;
// unclosed if statement at end of file
// footer.php:
if ($page == 'profile'):
// closing with } but opened with :
}else{
}
// stray endif, presumably intended to match the if: at the top of header.php
endif;
Upvotes: 1
Reputation: 647
There are two syntaxes in PHP for IF - THEN - ELSE. One is if (condition) :
... endif;
. The other one is if (condition) { } else { }
. Don't mix these. If you use the colon :
and endif;
, don't use curly braces { }
. If you use curly braces (recommended!), don't use colon and endif.
Inside your switch
statement, I recommend you put the default
at the end.
Upvotes: 3