Jump to content

Recommended Posts

Hi Guys,

I have a question, is it possible to leave an ELSE statement open within an Include file and end the ELSE statement later in the main page?

For example:

<?php

include 'PHP/db.php'; //include database info
include 'PHP/login-top.php'; //include PHP code for Login functions info

?>

<!doctype html>
<html>

<head>
	<meta charset="utf-8">
	<title>Untitled Document</title>
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

	<!-- Optional theme -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">

	<link rel="stylesheet" href="CSS/login.css">

	<!-- Latest compiled and minified JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
	<form class="form-signin" method="POST">
		<?php if(isset($fmsg)){ ?>
		<div class="alert alert-danger" role="alert">
			<?php echo $fmsg; ?> </div>
		<?php } ?>
		<h2 class="form-signin-heading">Please Login</h2>
		<div class="input-group">
			<span class="input-group-addon" id="basic-addon1">@</span>
			<input type="text" name="Email" class="form-control" placeholder="Email" required>
		</div>
		<label for="inputPassword" class="sr-only">Password</label>
		<input type="password" name="Password" id="inputPassword" class="form-control" placeholder="Password" required>
		<button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
		<a class="btn btn-lg btn-primary btn-block" href="register.php">Register</a>
	</form>
</body>

</html>
<?php
}
?>

At the moment I'm getting the following error:

Quote

Parse error: syntax error, unexpected '}', expecting end of file in C:\xampp\htdocs\BETA\login.php on line 46

But the } on line 46 is the closing of the ELSE { part within login-top.php include file. If i take all the code out of the include file (login-top.php) and place it in the same file as login.php it all works fine. But my aim to try and make the pages less cluttered and more easy to edit the design/HTML side of things which is why im placing the important PHP code in seperate files and including them when they need to be included.

Any help would be great, and thank you!

Link to comment
https://forums.phpfreaks.com/topic/308332-php-include-with-open-else/
Share on other sites

This is a problem I had long ago with the same error. Include files allow the php closing tag of ?> but for closing brace } to not be closed in the include files, you will get that error. To make it work, you will need to move all that code with the opening and closing braces into the code you showed above or close it with a closing brace inside the include file.

1 hour ago, requinix said:

...... An include means to run the file, not to "insert" the code at that point.

Perhaps I am misunderstanding you because you are  referencing the OP's post. Include does in fact "insert" the code at the point you include the file.  An included file may not have anything that "runs". It could just be some HTML. As the manual says,  include "evaluates the specified file". It is the same as though you cut and pasted whatever is in the include into the place the include is called.

So OP, to see what you are actually doing, cut and past the contents of the include as a replacement to the include line and you will see the code as PHP is seeing it.

Edited by benanamen
1 minute ago, benanamen said:

Perhaps I am misunderstanding you. Include does in fact "insert" the code at the point you include the file.  An included file may not have anything that "runs". It could just be some HTML. As the manual says,  include " evaluates the specified file ". It is the same as though you cut and pasted whatever is in the include into the place the include is called.

No, it does not work like that. Consider this:

<?php // one.php

if (false)
  include "two.php";
<?php // two.php

echo "first statement\n";
echo "second statement\n";

and compare with

<?php

if (false)
  echo "first statement\n";
  echo "second statement:\n";

 

If you include a footer before a header file, the footer is going to show up in source on top of the header and vice versa which clearly shows that an include is "inserted" where the include is called. Of course, the OP has some conditional thing going on, but the cut and paste I suggested will show how the code is not "syntactically valid " as you already mentioned.

As for your example using a conditional, the first one is saying "if true is false, then include the file". Since true is not false, it doesn't include it.

In the second example, you are not using curly braces, so only one statement is evaluated in the if, which is of the same effect as the first one, if true is false, then echo the first line". True is not false, so it doesn't echo the first line.

Seems we might not be on the same page. Kindly explain what you wanted me to see about the example. FYI: You did get me to learn up on something. Since I always use curly braces I didn't know without them only one statement is evaluated.

27 minutes ago, benanamen said:

If you include a footer before a header file, the footer is going to show up in source on top of the header and vice versa which clearly shows that an include is "inserted" where the include is called.

Executed. Not inserted. PHP did not perform some sort of virtual copy-and-paste of the included file's contents into the point where the include call was. What PHP did was execute the file.

Quote

Seems we might not be on the same page. Kindly explain what you wanted me to see about the example.

You said that an include "is the same as though you cut and pasted". I'm picking at how and why that statement is factually incorrect.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.