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 :: generate fake name php 
Php :: How do you set a variable to an integer? in php 
Php :: execute function php 
Php :: how to assign variable in php 
Php :: phpmyadmin drop database 
Php :: php get filename 
Php :: array marge in php 
Php :: laravel route with multiple parameters 
Php :: update cart subtotal woocommerce 
Php :: two column date compare in php 
Php :: woocommerce get shipping classes 
Php :: routes not defined 
Php :: jsondecode php array 
Php :: wordpress popular posts query 
Php :: laravel get route path uri 
Php :: close connection pdo 
Php :: pagination in codeigniter with example 
Php :: laravel belongs to 
Php :: acf add options page to custom post type 
Php :: php globals 
Php :: PHP strrpos — Find the position of the last occurrence of a substring in a string 
Php :: woocommerce disable links on specific product 
Php :: use session in laravel 
Php :: laravel basic login 
Php :: laravel faker 
Php :: return back laravel controller 
Php :: using get in laravel blade 
Php :: php photo upload 
Php :: laravel request file empty 
Php :: catch warning php 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =