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
Upgrading Concrete from 8.5 to 9.x
Jun 21, 2024
By myq.

How to avoid problems upgrading from 8.5 to 9.x

How to change the default date format
May 30, 2024
By myq.

Change the format of the default date

WebOps Tutorial on Running and Upgrading a Concrete CMS Website
May 2, 2024
By myq.

Describes how to deploy, manage, and upgrade a Concrete CMS website

Using the Concrete Migration Tool Addon
Apr 27, 2024

How to use the Concrete CMS Migration Tool

Create custom Site Health tasks
Apr 19, 2024
By myq.

This tutorial will guide you through the creation of a new Site Health task

Reusing the same Express entity in multiple associations
Apr 11, 2024
By myq.

How to create and manage multiple associations in Express

Improvements?

Let us know by posting here.