Syntax Problem

Sorry, I’m kind of a newbie and this is a simple problem for someone who knows the syntax well.

I have code like this to link a file to my main document. This code works

<script src="<?php if (isset($absolutePath)) {echo "{$absolutePath}";} ?>/scripts/js/banner.js" type="text/javascript"></script>

And I have code like this to include a document. This code also works…

<?php include($filePath . $bridge . 'scripts/includes/header.inc.php'); ?>

I want to use the isset command with the include. This code doesn’t work.

<?php include if (isset($filePath)) {echo "{$filePath}"} . if (isset($bridge)) {echo "{$bridge}"} . 'scripts/includes/aboveHeader.inc.php'); ?>

I am getting a T_IF error.

Parse error: syntax error, unexpected T_IF in /home/content/p/a/u/paulmcrae/html/siteBuilding/mcraeCombustion/index.php on line 27

What am I doing wrong?

if can only be used as separate statement, and can not occur within another statement such as include.
That being said you could use the ternary operator here:


include
  (isset($filePath) ? $filePath : '') .
  (isset($bridge) ? $bridge : '') .
  'scripts/includes/aboveHeader.inc.php';

(spread of multiple lines for readability, it will also work if you put it all on one line)

Thank you for your help ScallioXTX!

If I understand correctly, in this case the ternary operator is saying if $filepath exist output $filepath. If not output nothing because the second argument is an empty string?

I have another somewhat related problem. I have these variables listed at the top of the document like so…

<?php 
	$currentPage = basename($_SERVER['SCRIPT_NAME']);
	$filePath = $_SERVER['DOCUMENT_ROOT'];
	$bridge = "/siteBuilding/mcraeCombustion/";
	$absolutePath = 'http://www.paulmcrae.com/siteBuilding/mcraeCombustion'; 
?>

I would like to put these variables into an include so I can update them in a single place when I transfer the files to a live server. The catch 22 of course is that I can’t use these variables to call the include which contains these variables. How might I solve this problem?

Yes, that it’s exactly what it’s saying :tup:

Yes, you can generalize a lot of things when it comes to paths, but you have to make an assumption in bootstrapping; the index needs to know where it can find the config file.
I use dirname(FILE) to refer to the config file in the index. The config file is 1 dir up in my setup.


/public_html
   /index.php
   /.htaccess
   /(etc)
/config.php
/application-stuff

(not actual directory names of course, you get the idea)

so in index.php I use:


include(dirname(__FILE__).'/../config.php');

Now all that is assumed is the relation between the index and the config (namely, the config is one dir up from the index) without exactly specifying where it is (no physical path).

Does that answer your question?

I’m trying to understand what you have written, but I’m not certain. I think you are suggesting that dirname(FILE) would be a better way to define the file path than creating a variable. Still, the dirname(FILE) resides in the index file?

Or are you saying use dirname(FILE) to call the config file only and use the variables for file paths in the document?

Or am I missing it altogether?

And the config file lives in the root folder? Is it necessary to place the config file in the root folder or can it go anywhere?

Yes, that’s what I’m saying :slight_smile:

I always put the config file in a location that cannot be server by the web server. If possible that is. If it’s not possible I put it in a directory I deny all access to with a .htaccess deny from all

Thank you for your help and advice.

You’re welcome. If anything isn’t clear or if you have more questions, feel free to come back to this thread (or start a new one of course) :slight_smile: