Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Saving Paths in Variables

Featured Replies

I'm still fairly new to PHP so bear with me here unsure.gif. Is there a way I can save pathnames in variables or something similar to try and keep all my include paths and image paths in one easily accessible page? I've tried googling for an answer and searching on the PHP manual but unfortunately my problem is also that I wouldn't know how to put this issue into a searchable phrase. Well on to the issue at hand...here is an example. I have an image:

 

<a href="images/foods/foods_rotisserie/foods_rot_pollo_entero.png" rel="lytebox" title=""><img src="images/foods/foods_rotisserie/foods_rot_pollo_entero.png" alt="Pollo Entero" /></a>

 

I would like to change it to something like:

 

<a href="<?php echo $path ?>/foods_rot_pollo_entero.png" rel="lytebox" title=""><img src="<?php echo $path ?>/foods_rot_pollo_entero.png" alt="Pollo Entero" /></a>

 

I've experimented with several different methods but none of them worked out so great. Is there a way I can have a list of variables each with a different path name that I can call when I need them? For example at the top of my page I'd call a function that holds all the variables.. i tried something like this

function imagePaths() {
$path1 = "PATH_NAME";
$path2 = "PATH_NAME";
// and so on and so forth...
}

 

That didn't work very well. I'm just looking for a way to hold all my paths names in one file to make it easier on me. I've been thinking about arrays but unfortunately I don't really know how those work and if it would work... any input is greatly appreciated. Thanks in advance!

I'm not an expert with PHP but I have a working example of what you want so if you can take my example and change the relevant bits to your code, it should work.

 

<div id="picture"><?php echo "<img src='".$sel_subject['image']."'>"; ?><br></div>

You are on the right track...

 

Here is an example using arrays and a function. I'm passing the array key as a parameter and the function will return the value. Probably worth extending this to check if array_key_exists otherwise you might get a PHP warning if you lookup a path that doesn't exist.

 

   function getPath($image) {
	$paths = array(
		'rotisserie' => 'images/foods/foods_rotisserie/',
		'ham' => 'images/foods/foods_ham/',
		'pork' => 'images/foods/foods_pork/'
	);    

   	return $paths[$image];
   }

 

Usage:

 

//short tags
<?=getPath('ham')?>

//normal
<?php echo getPath('rotisserie');

  • Author

Thank you all for the replies. I think I get the basic idea and I'm going to try experimenting with Jock's example. It pretty much hits the nail on the head to what i was trying to achieve. While the site was down I decided to give it a go one more time and I finally got it to work. It works but there's definitely too much code needed for something that can be achieved so easily lol

 

function imagePaths($dir) {
// Foods A La Brasa
if($dir == "foods_alb") {
	$dir = "inspiration/";
	echo $dir;
}
// Foods Rotisserie
elseif($dir == "foods_rot") {
	$dir = "inspiration/mehler/";
	echo $dir;
}
}
function includePaths($inc) {
// Header
if($inc == "header") {
	require ("header.php");
}
// Footer
if($inc == "footer") {
	require ("footer.php");
}
}

 

These are the examples that call the functions:

 

<img src="<?php imagePaths("foods_rot"); ?>image.png" width="300" height="300" alt="Image" /><br /><br />
<?php includePaths("header"); ?>
<?php includePaths("footer"); ?>

 

No worries though I'm not going to use this one. I think using arrays would be easier than that.

 

You could also simply create a constant is your includes file E.g.

 

define('IMAGE_DIR', '/files/imges/');

 

I'm going to look into this as well. Seems like an even easier approach to this... and Quake Live rocks man! :D

define() is pretty much just '$var =' and doesn't have much to do with what you're looking to achieve. Jock's method is definitely the way forward (though I would perhaps use a switch statement with a default value instead of an array).

define() is pretty much just '$var =' and doesn't have much to do with what you're looking to achieve. Jock's method is definitely the way forward (though I would perhaps use a switch statement with a default value instead of an array).

Just to clarify what Shaun meant (if you're new it doesn't help getting the wrong idea!):

define('CONSTANT_NAME', 'constant_value') will define a constant that is available everywhere after it is, well, defined. $var = 8; on the otherhand would mean $var is only available in the current scope. Might be worth checking the php manual on variable scope if that's confusing.

 

The function route that Jock suggested is definately the easiest though. I'd also recommend using an array as it's easier to update and understand what's going on.

function imagePaths($dir) {
       $paths = array(
           'foods_alb' => 'inspiration/',
           'foods_rot' => 'inspiration/mehler/'
       );
       // return some default thing if we don't know the array key
       if (!isset($paths[$dir])) {
           return 'images/';
       }
       return $paths[$dir];
   }

Aye, that's why I said 'pretty much'.

 

To be fair, 'key' -> 'value' isn't too different from case : var.

 

function imagePaths($dir) {
       switch($dir){
           case 'foods_alb' : $op = 'inspiration/'; break;
           case 'foods_rot' : $op = 'inspiration/mehler/'; break;
           default : $op = 'images/';
       }
       return $op;
}

 

Personally, I would only use an array if I was handling a lot more data, or if I had other uses for the data. Bear in mind, in this case they do the exact same thing.

 

In Rob's code, I would change the end of the function to:

 

} else {
       return $paths[$dir];
}

  • Author

oh nice using switch makes sense as well im using switch to help manage my pages easier along with includes for the footer, header, etc. i'm going to try using arrays since i've never experimented with them before and i plan on having a lot of paths saved into this array. i appreciate all the input and example you guys gave me and also the explanations. great forum!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.