STRINGS - Combine one or more variables and text strings inside view.php

This is a community-contributed tutorial. This tutorial is over a year old and may not apply to your version of Concrete CMS.
Mar 22, 2016
By siton for Developers

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! 
?>

Concrete CMS 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:

Recent Tutorials
Edit domains and sitemaps
Apr 4, 2025
By myq.

How to create a sitemap when using an edit domain

Block Types and CIF Data
Apr 2, 2025
By mlocati.

This tutorial describes how Concrete works with blocks data, and how you can create custom block types that works well when exporting and importing data with the CIF XML format.

Customize the default page title
Mar 12, 2025

Change the default " :: " and/or "site name :: page title" formatting separator to something else.

Configure Composer to work with a Page Type
Feb 20, 2025
By myq.

Fix the "Unable to load block into composer. You must edit this content from within the context of the page." error message

Permissions for editors in a multilingual site
Feb 2, 2025
By myq.

How to set up a multilingual Concrete CMS site for groups of language-specific editors

Restoring deleted pages using advanced search
Jan 16, 2025
By myq.

How to recover deleted pages when there are more than a few to choose from.

Improvements?

Let us know by posting here.