2

In PHP we echo strings this way :

echo 'string';

But i saw PHP frameworks like Laravel and scripts echo strings using Curly Brackets :

{string}

How i can do that without using any PHP framework?

It's not necessary to use Curly Brackets if there is other way to short echo!

I prefer code examples.

Amr SubZero
  • 1,196
  • 5
  • 19
  • 30
  • Laravel uses the template engine `Blade` (by default), thats why the syntax is not the same as in php. – Jite Nov 22 '14 at 06:19
  • I don't use PHP anymore, but perhaps you could look through the document for a specific match with Regex, then echo anything between the `{}`. –  Nov 22 '14 at 06:21

4 Answers4

7

PHP has a few methods to print strings, such as (but not limited to) print, and echo or just shorthand <?= "str" ?>.
The bracket print that you ask about from laravel is not per say in php.
That is from a template engine called Blade.

So the {} way of printing stuff is not possible in php.
You will have to stick to the standard ways or use a template engine!

Jite
  • 5,761
  • 2
  • 23
  • 37
  • But i saw a php script (not a framework) using that way to echo {string} and also working in html files! thanks for your information. – Amr SubZero Nov 22 '14 at 06:39
  • 2
    Was the name of the php file: somename.blade.php? :) – Jite Nov 22 '14 at 06:41
  • Nope, it was named like file.html – Amr SubZero Nov 22 '14 at 06:42
  • Its possible to make the engine look for php or html files (without the blade part) as blade templates. I have not tried this, so I'm not 100% sure on how it works, and I would not recommend this, cause its a lot more easy to see that its a blade template if its in the file name! :) `View::addExtension('php', 'html');` – Jite Nov 22 '14 at 06:46
5

You can short echo by

<?= $variable; ?> when you open a new php tag

or you use, as described in your question a framework such as laravel - that's pretty much it

What you can do, but I'm not really sure if it's a good idea - write a function like:

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

x('Test');  // will output Test
baao
  • 71,625
  • 17
  • 143
  • 203
  • Away from talking about Laravel, i saw a php script using that way to echo {string} .. i think it's a way of coding or a plugin/engine but i really don't know what is it! the function solution is good by the way! – Amr SubZero Nov 22 '14 at 06:29
  • Even though its possible to create a shorter function, I would not recommend doing so, it will make the code harder to read and understand, which is not a good thing. (And yes, I noticed that the answer said it might not be a good idea, I'm just supporting that!) – Jite Nov 22 '14 at 06:31
  • Thanks! Not sure about the {}, I haven't seen it yet outside of laravel and MVCs – baao Nov 22 '14 at 06:31
2

The short answer is you can't do that with PHP. PHP provides language constructs to output to standard output, etc. PHP doesn't provide a pre-processor. Most template frameworks are a pre-processor meaning that they convert your {<STRING>} into an echo $x statement.

So either create your own template framework, or stick to PHP's API.

Ryan
  • 14,392
  • 8
  • 62
  • 102
  • about creating my own template, i just wanted to do that echo way {string} not creating a php framework from scratch, and i swear god i saw a php script (not framework) using that way to echo {string} and also working in html files! thanks for your information. – Amr SubZero Nov 22 '14 at 06:37
  • Link to PHP script...? – Ryan Nov 22 '14 at 06:43
  • Here's the script, go to the "Templates" directory and open for example "links.html" you'll see he's using {} to echo : http://goo.gl/JWGPL6 – Amr SubZero Nov 22 '14 at 07:03
  • Sorry, here's the link of the script : http://www31.zippyshare.com/v/17537696/file.html – Amr SubZero Nov 22 '14 at 07:13
0

After researching i found a Template Engine called Smarty that allows me to echo using Curly Brackets

First i created 2 files index.php , template.html

Then i moved the folder /libs that i downloaded from the template page to my script directory

and in my index.php i required the Smarty.class file

require_once "libs/Smarty.class.php";

then i called the Smarty class :

$smarty = new Smarty;

after that i pushed the variable :

$smarty ->assign("name",'Amr');

and i displayed the template file index.html

$smarty ->display("template.html");

at the end i wrote in the index.html <p>{$name}</p> and it outputted : Amr

Amr SubZero
  • 1,196
  • 5
  • 19
  • 30