Search
 
SCRIPT & CODE EXAMPLE
 

PHP

template literals php

<?php
// Show all errors
error_reporting(E_ALL);

$great = 'fantastic';

// Won't work, outputs: This is { fantastic}
echo "This is { $great}";

// Works, outputs: This is fantastic
echo "This is {$great}";

// Works
echo "This square is {$square->width}00 centimeters broad.";


// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";


// Works
echo "This works: {$arr[4][3]}";

// This is wrong for the same reason as $foo[bar] is wrong  outside a string.
// In other words, it will still work, but only because PHP first looks for a
// constant named foo; an error of level E_NOTICE (undefined constant) will be
// thrown.
echo "This is wrong: {$arr[foo][3]}";

// Works. When using multi-dimensional arrays, always use braces around arrays
// when inside of strings
echo "This works: {$arr['foo'][3]}";

// Works.
echo "This works: " . $arr['foo'][3];

echo "This works too: {$obj->values[3]->name}";

echo "This is the value of the var named $name: {${$name}}";

echo "This is the value of the var named by the return value of getName(): {${getName()}}";

echo "This is the value of the var named by the return value of $object->getName(): {${$object->getName()}}";

// Won't work, outputs: This is the return value of getName(): {getName()}
echo "This is the return value of getName(): {getName()}";

// Won't work, outputs: C:folder{fantastic}.txt
echo "C:folder{$great}.txt"
// Works, outputs: C:folderfantastic.txt
echo "C:folder{$great}.txt"
?>
Comment

template string php

You could also use strtr:

$template = '$who likes $what';

$vars = array(
  '$who' => 'tim',
  '$what' => 'kung pao',
);

echo strtr($template, $vars);
Outputs:

tim likes kung pao
Comment

PREVIOUS NEXT
Code Example
Php :: type hidden value put laravel 
Php :: concat in php 
Php :: php gethostname 
Php :: write php online 
Php :: woocommerce order item get product id 
Php :: file exist php 
Php :: shortcode php wordpress 
Php :: lDownload multiple files as a zip-file using php 
Php :: check mobile or email in laravel 
Php :: Catches the last error php 
Php :: array_column in php 
Php :: yii2 clear schema cache 
Php :: php microtime to ms 
Php :: php array order alphabetically 
Php :: in_array validation laravel 
Php :: root composer.json requires php ^7.3 but your php version (8.0.3) does not satisfy that requirement. 
Php :: add json extenstion php 
Php :: laravel fetch max value 
Php :: php redirect with query string 
Php :: php json get value by key 
Php :: php-fpm docker 
Php :: php http errorcode 
Php :: make exception laravel 
Php :: append data in csv file php 
Php :: Stored Procedures in Laravel 
Php :: laravel request get parameter 
Php :: php datetime from timestamp 
Php :: use php var in js 
Php :: login selected user laravel 
Php :: excerpt with Laravel 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =