Jump to content

PHP Short tags question


renfley

Recommended Posts

Hey guys i was wondering is there is any solutions for what i wanna do.

 

Something Like this

 

    function Tag(){

    $title="Site Name";

    }

 

I want to do this....

 

    <title>{$title}</title>

 

Does anyone know what this method is called and if this even uses php or maybe even javascript?

 

Link to comment
https://forums.phpfreaks.com/topic/279194-php-short-tags-question/
Share on other sites

It's templating. Heard of Smarty? That, and others like it. Basically it works by having some PHP code that "evaluates" the template by doing things like "find all instances of {var} and replace them with the value of var". And potentially more advanced stuff.

 

It's web development for people who can't write PHP.

$title = "Site Name"; echo "<title>$title</title>"; would be easiest, no need to use a function.

 

If you want to fire a dynamic title change event, use JavaScript.

 

This function, for example.

function setTitle(title) { document.title = title; }

And later on, you can register the function to various event handlers, for example, the .click() jQuery method. Example below.

 

jQuery('#title_button').click(function(e){
e.preventDefault(); // prevent eventual predefined click events, if it is a link or so
setTitle(jQuery(this).html()); // title now becomes the button's innerHTML,
// but you can set it to any string value you want to,
// and consider using the toString() methods
});

ive decided to save the static informaiton in an array which i have created but havent figured out how to print it, 

$config = array(
		"mysql" => array(
			"DB_NAME" => "dbname",
			"DB_USER" => "dbuser",
			"DB_PASS" => "dbpass",
			"DB_HOST" => "dbhost",
			),
		"paths" => array(
			"resources" => "/public_html/",
			"images" => array(
				"content" => $_SERVER["DOCUMENT_ROOT"] . "/images/content",
				"layout" => $_SERVER["DOCUMENT_ROOT"] . "/images/layout"
				)
			),
		"title" => array(
			"title" => "Sitename",
			)
		);

So you can see ive save the title in the title array which will work for the task at hand. 

 

My  issue is when i normally echo  out a array 

echo $i['user'];

but in this case the array is in the $config var, How would i go about echoing out the title?

Why the second dimension for the title? You could just as easily have just assigned it to the first dimension, seeing as there is only one element in there.

 

That said, what you have there is a two-dimensional array. How to access element in the second (or n'th) dimension is nicely explained in the PHP manual (see example #6).

I am currently writing a framework and am experimenting with different array configurations,  which is why tried the second Dim...

 

I've pretty much gone back to my original method which runs smoothly.

        //Here is the database information for use throughout the site. 
	//You can use these anywhere -- echo $DB_USER
	$mysql = array('DB_NAME' => 'dbname' , 'DB_USER' => 'dbuser', 'DB_PASS' => 'dbpass', 'DB_HOST' => 'dbhost'         );
	
	//this will take all the keys in the array and allow you to parse them as a variable.
	// echo $DB_USER;
	foreach($mysql as $key=>$value) { $$key = $value; }
		

I can then echo any of the keys are variables,

<?php
    echo $DB_USER;
?>

which will output dbuser.

 

This has always been my go to method of doing it but wanted to see other methods and realized there aren't really any other ones that are as simple and as effective. 

 

Thanks Guys for all the help!

Using variable variables is bad form regardless of the case, and using variables for configuration values (which tend to be constants) is not recommended either.

What I do recommend you to do, is to define constants for the configuration settings. That way you can access them wherever you like, and you get them clearly defined as something different from runtime data.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.