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
Upgrade Concrete versions 9.3.1 and 9.3.2
Sep 10, 2024
By myq.

How to get past a bug in versions 9.3.1 and 9.3.2 that prevents upgrading the Concrete core through the Dashboard

How to use Composer with Marketplace extensions
Aug 22, 2024

Composer can be used to manage third-party extensions from the marketplace

Controlling Google Tag Manager Tags Based on Concrete CMS Edit Toolbar Visibility
Aug 13, 2024

This document provides a step-by-step guide on how to control the firing of Google Tag Manager (GTM) tags based on the visibility of the Concrete CMS edit toolbar. It explains how to create a custom JavaScript variable in GTM to detect whether the edit toolbar is present on a page and how to set up a trigger that ensures GTM tags only fire when the toolbar is not visible. This setup is particularly useful for developers and marketers who want to ensure that tracking and analytics tags are not activated during content editing sessions, thereby preserving the accuracy of data collected.

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

Improvements?

Let us know by posting here.