0

so I have an array, the array is:

<?php
$config = array('site_name' => 'SomeName');
?>

Now I want to echo it.

<title><?php echo $config['site_name']; ?></title>

But I have seen some websites using:

<title>{site_name}</title>

Is it possible to do so? If so can you help me out?

user3152750
  • 128
  • 1
  • 11
  • 1
    This is template syntax. I guess either smarty or twig. – hek2mgl Jan 17 '14 at 11:56
  • 3
    Search for a PHP template engine. – Markus Malkusch Jan 17 '14 at 11:56
  • 1
    no you cant use it by php. You can attain this by using smarty – Anish Jan 17 '14 at 11:56
  • Have a look at this https://github.com/bobthecow/mustache.php this may interest u. – Abhik Chakraborty Jan 17 '14 at 11:57
  • 1
    you also use =$config['site_name']?> with short_open_tag enabled in php.ini – aconrad Jan 17 '14 at 11:57
  • aconrad that was a good one, but I want something like that to shorten my code, plus other's who are suggesting me a template engine, I am making my own...... – user3152750 Jan 17 '14 at 12:06
  • This question appears to be off-topic because it is asking for personal support. – hakre Jan 17 '14 at 13:39
  • I am not asking for personal support @hakre I am looking for the source of {} brackets so everyone can shorten their code and utilize the code. – user3152750 Jan 18 '14 at 06:31
  • You must have confused things, this is not pure PHP what you're asking about. Which btw. would is documented here: http://php.net/docs - Which brings me to the point that you should might have thought about that not finding the source might be a hint that the source is not where you think it is and this probably isn't PHP even and you should have made that more prominent while asking, otherwise it's just too broad. – hakre Jan 18 '14 at 13:17

1 Answers1

5

That syntax is not a valid syntax to echo a statement with PHP, it's the smarty framework who uses that, which is nothing but a template engine.


Also, don't confuse that syntax with something like

<title><?php echo "This is a {$title[0]}"; ?></title>

Which is a valid syntax in PHP, but you need to use the curly braces here, as you are echoing an index of an array.. For more information, you can read an answer here..


As you commented that you want to shorten up your codes, than an alternate way to echo in PHP is to use <?= but before that, do have a read here.

Or else, declare a function with shortest name like

function e($string) {
    echo $string;
}

e('Echo This String'); //Or
e($string);
Community
  • 1
  • 1
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • I am doing this to shorten the code, not to extend the code. – user3152750 Jan 17 '14 at 12:06
  • @user3152750 So where am saying you to even extend your code? I just explained what you asked.. any mistakes? – Mr. Alien Jan 17 '14 at 12:06
  • He doesn't really need the curly braces there. It's needed when trying to fetch array values inside of double quote strings. – Madara's Ghost Jan 17 '14 at 12:10
  • I want to just fetch the array stuff in braces {} so i dont have to write – user3152750 Jan 17 '14 at 12:14
  • @user3152750 Am not getting you... I just edited my answer, you can refer that if it helps you or simply ignore :) – Mr. Alien Jan 17 '14 at 12:14
  • Errrr I simply mean, that I want to replace the text " to just {site_name} – user3152750 Jan 17 '14 at 12:22
  • 2
    @user3152750 You are going too fast, learn PHP first... – Mr. Alien Jan 17 '14 at 12:29
  • Er excuse me? I do know PHP, and I am trying to widen my knowledge in PHP day by day..... :( – user3152750 Jan 17 '14 at 12:33
  • 1
    @user3152750 Err, I'd recommend you stick with native PHP; template engines for `{replacements}` are slower and often feature-limiting. If you can't do it with native, then you can't do it anyway. – Dan Lugg Jan 17 '14 at 12:35
  • 1
    @user3152750 Am not doubting your knowledge but you are unable to understand the construct here, you are saying that my answer is [incorrect](http://stackoverflow.com/questions/21185360/php-curly-bracket-parsing/21185395?noredirect=1#comment31897636_21185419) but you are asking me to explain you how to build a google search engine, without you learning relevant programming languages – Mr. Alien Jan 17 '14 at 12:35
  • ......................... @Mr.Alien – user3152750 Jan 19 '14 at 03:34