Question about Require/Include

When you have a Require/Include file, does it have to have a logical beginning and end like this…


<?php
	$extendedPrice = $unitPrice * $quantity;
	echo "The Extended Price is " . $extendedPrice;
?>

or can it just pick up anywhere like this…


		if (empty($_SESSION['loggedIn']) || $_SESSION['loggedIn']===FALSE){
			// Not Logged In.
			$_SESSION['resultsCode'] = 'PROFILE_NO_QUERY_STRING_2143';

			// Set Error Source.
			$_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME'];

			// Redirect to Outcome Page.
			header("Location: " . BASE_URL . "/account/results.php");

			// End script.
			exit();

I also have the same question if the content is HTML…

Thanks,

Debbie

PHP files that are to be included must begin their PHP tags at the beginning of the code to be executed.

When Include (or Require) are called, the interpreter reverts to HTML mode (AKA: It interprets a ?> ), reads and runs the file. When it gets to the end, it re-enters PHP mode (AKA: puts a <?php in ), and continues parsing as normal.

The included file inherits the scope of the line it is called on; normally this is the global scope, but it could be called inside a function, for example.

the engine makes no attempt to interpret HTML code (it never does). If your headers/footers are missing, it will be the BROWSER that adds them, not PHP.

I don’t follow you…

Lemme try again…

Here is a snippet of a script that I am working on…


		// ************************
		// Find Member Record.		*
		// ************************

		// Connect to the database.
		require_once(WEB_ROOT . 'private/mysqli_connect.php');

		// Build query.
		$q1 = 'SELECT id
				FROM member
				WHERE username=?';

		// Prepare statement.
		$stmt1 = mysqli_prepare($dbc, $q1);

		// Bind variable to query.
		mysqli_stmt_bind_param($stmt1, 's', $user);

		// Execute query.
		mysqli_stmt_execute($stmt1);

		// Store results.
		mysqli_stmt_store_result($stmt1);

		// Check # of Records Returned.
		if (mysqli_stmt_num_rows($stmt1)==1){
			// Member was Found.


			// ************************
			// Determine Active Tab.	*
			// ************************
			if (isset($_GET['tab']) && $_GET['tab']){
				$tab = strtolower($_GET['tab']);

				if ($tab == 'about-me'){
					// Build out "About Me" tab.
				}elseif ($tab == 'my-thoughts'){
					// Build out "My Thoughts" tab.
				}elseif ($tab == 'my-friends'){
					// Build out "My Friends" tab.
				}else{
					// Build out "About Me" tab.
				}
			}else{
				// Build out "About Me" tab.
			}

		}else{
			// Member Not Found.
			$_SESSION['resultsCode'] = 'PROFILE_MEMBER_NOT_FOUND_2144';

			// Set Error Source.
			$_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME'];

			// Redirect to Outcome Page.
			header("Location: " . BASE_URL . "/account/results.php");

			// End script.
			exit();
		}//End of FIND MEMBER RECORD

See the header “Determine Active Tab”?

What if I wanted to put all of that code in a require/include?

Would that work?

And what if I wanted to put all of the code that will go under “// Build out “About Me” tab.” and place that in a require/include?

Would that work?

Because I am NOT doing OOP this go around, I am trying to break up a file that is getting very large very quickly now that I need to include code for populating 3 Tabs?! :eek:

I thought using require/include files might help keep things more manageable…

Follow me?

Debbie

I think popping that code in a function would be better if you want to re-use it, given it uses nothing surrounding it.

Could you do it? Sure. You’d need PHP tags around the code in the file, but you could do it.

You could have an entire file whose entire contents were <?php include_once(‘someotherfile.php’); ?>

It’d be a bit silly, but you could do it.

The file you’ve posted is not, in my opinion, large… depending on what you mean when you put ‘build out … tab’.

You could certainly do includes there, you could even do them programmatically if you name the files in a likewise fashion to the variable name, or with a function if the code for the tabs were the same…

I was wondering about that as well.

See my response to StarLion below and tell me what you think…

Thanks,

Debbie

Let me explain the bigger picture here…

I have a script called “profile.php” which is a Member’s Profile.

It includes a Page Header with navigation, welcome, account mgt links, etc. In the left margin is a whole bunch of stuff like the Member’s photo, links to contact the Member, listing of Friends, Visitors, etc.

In the center of the page are 3 tabs: About Me, My Thoughts, My Friends. (This will likely grow.)

I have everything hard-coded and working for when you land on this page which includes the “About Me” tab.

Now I am trying to split things up so depending on the URL, I can display one of the three tabs.

For example…

http://local.debbie/account/profile.php?user=DoubleDee&tab=about-me

http://local.debbie/account/profile.php?user=DoubleDee&tab=my-thoughts

http://local.debbie/account/profile.php?user=DoubleDee&tab=my-friends

Looking at my code, there are two areas where I need to “plug-and-play”…

1.) At the top of my script in the PHP section, I have my Prepared Statement that gathers the needed data for a given Tab.

2.) At the bottom of my script in the HTML section, I have PHP and HTML which displays the data gathered in #1.

So, for #1, I am adding this code…


			// ************************
			// Determine Active Tab.	*
			// ************************
			if (isset($_GET['tab']) && $_GET['tab']){
				$tab = strtolower($_GET['tab']);

				if ($tab == 'about-me'){
					// Build out "About Me" tab.
				}elseif ($tab == 'my-thoughts'){
					// Build out "My Thoughts" tab.
				}elseif ($tab == 'my-friends'){
					// Build out "My Friends" tab.
				}else{
					// Build out "About Me" tab.
				}
			}else{
				// Build out "About Me" tab.
			}

…and the plan is to insert a Prepared Statement for each branch as commented above.

And for #2, I would do a similar IF-THEN-ELSE to determine which PHP/HTML I need to display the relevant Tab.

Now I could do all of this in one file, but I am already up to 400 lines of code with the default “About Me” code, so if I add in “My Thoughts” and “My Friends” this file could easily double and that is too big in my mind.

So I am looking for the best way to “plug-and-play” the Prepared Statements and Display code I need for each Tab.

I’m sure this is am example of where if I was using OOP it might be easier, but since that is NOT an option for this release, I need to find an eloquent way of doing things procedurally.

Make sense?

Thanks,

Debbie


$tab = isset($_GET['tab']) ? $_GET['tab'] : 'about_me'; //Needs sanitizing.
if(file_exists('memberfiles/'.$tab.'.php') {
 include('memberfiles/'.$tab.'.php');
} else {
 include('memberfiles/about_me.php');
}

Will save you having to write out long if/else’s :wink: