This issue is for beginners - Basic PHP Strings.

1.View.php - Mix text string & variables

A lot of times when you want to create a custom block template you need to change the core block markup and change/add css declerations inside your duplicate view.php.

Mabye you find a JS slider and you need to change the core image slider block output from:

   <div class="ccm-image-slider">
    ...rest of the code

To:

   <div class="my-slider-container my-image-slider blabla">
    ...rest of the code

If you are new to PHP i believe you get a lot of wierd small errors in this procces, so i hope this tutorial will give you ideas where to look in your code.


2.The problem - "unexpected (T_VARIABLE)"

Single and double quotes

If you dont want to insert $vars values inside your string its ok to use either echo 'hello world'; or echo "hello world";. But if you inserting variable values inside Single quotes the $var will not parse (php will print $var as a regular text). See example 1-5.

//Example 1-5. single-quotes & double-quotes
$example = "My Example";
echo 'single-quotes-$example'; // Output: single-quotes-$example (var not parse)
echo "Double-quotes-$example"; // Output:Double-quotes-My Example (var is parse)

Echo function, vars and Double quotes

The echo function uses quotes ("") to define the start and end of a string. But also in html markup we uses quotes ("") to define the start and end of css declaration class="myClass" as shown in Example 2-5.

//Example 2-5. Syntax error beacuse of the quotes around $myClass
<?php
$myClass="myClass";
echo "<h1 class="$myClass">Hello world</h1>"; //Wont work! 
?>

Concrete5 throw error:

syntax error, unexpected '$myClass' (T_VARIABLE), expecting ',' or ';'


3. Two Ways to solve this issue

3.1. Wrong approach - "Echo spagethie"

<?php
//Example 3-5. Correct example with a lot of echo statements
$myClass = "myClass";
echo "<h1 class="; echo $myClass; echo">Hello world</h1>"; 
?>

In 3-5 Example - We solve the problem by adding some echo statements. Its works fine, But the code is now "dirty", less readable, and easy to get confused in the procces.


3.2. Eascape quotes

Quotes are speacial character. Escape quotes within the string with a slash. How it works? You escape a character by typing a "slash" before it `\"$myClass\".

<?php
//Example 4-5. Correct example with escaping of special characters
$myClass = "myClass";
echo "<h1 class=\"$myClass\">Hello world</h1>"; 
?>

3.3. Concatenation operator

Combine one or more variables and text strings. Concatenation Save you a lot of echo statements for clean and readable code.

  • Step 1: Use single quotes (') for quotes inside your string '<h1 class="'

  • step 2: Add Concatenation '. $myClass .'

  • Step 3: Use single quotes (') for quotes inside your string '">Hello world</h1>'; '

<?php
//Example 5-5. Concatenation strings and variables together
$myClass = "myClass";
echo '<h1 class="' . $myClass . '">Hello world</h1>'; 
?>

4. Read about this issues in PHP official docs:

Loading Conversation