require_once()?

I have a question about this function.
I’m using a MySQL database so I created a file named db_conn.php so it can be called wherever needed.
I have…


<?
if($_POST['name'] != "") {
require_once("db_conn.php");

session_start();
$user = $_POST['name'];
$pass = $_POST['pass'];
$result = mysql_query("SELECT * FROM users WHERE name = '$user' AND password = '$pass'") or die(mysql_error()); 
$row = mysql_fetch_array( $result );
}
?>

<span id="navbar">
<?php
if($row) {
$_SESSION['id'] = $row['id'];
header("Location: editable.php");
exit;
} else {
?>

I was having issues when the db_conn.php page had no opening and closing PHP tags, but once I added them, the page worked good, why did I need those tags?

No matter what you need an opening PHP tag in every *.php file, think of the tag as been an identifier for that file. If we include the file into our script without an opening PHP tag the mime type would be type/text allowing the dump to just output to the page, if we add that opening tag the mime type then changes to type/application-php.

Personally i don’t recommend using PHP short tags (<?) as to me it doesn’t represent PHP where as (<?php) makes more sense and is not mistaken as something else, also i have seen a lot of time in the past where the short tag can easily be mistaken for an XML tag which is the main reason i don’t like them.

If you don’t specify those tags, then it becomes normal text content.
Normal text content cannot be interpreted by PHP, that’s way your getting the error.

It is an annoying part of PHP – a piece of legacy I REALLY think should be removed from the specification ENTIRELY… at least then it would stop people from opening and closing php on every blasted line turning their code into a needlessly complex train wreck.

Though to put that in perspective, I say the same thing about how people use double quotes for EVERYTHING.

All php files that will be executed as separate files need those starting and endings. When you use require_once, the functions and everything included there are loaded in to memory and its not copying the code to the new file.

You don’t need the end tag when it comes at the very end of the file.

You should also have a look at why PHP short open tags are bad.