Skip to content
On this page

prefer-single-quotesfixable

Ensure that double quotes are converted to single quotes where possible

Examples

  • Examples of correct code for this rule using default options

Echo, variables, function calls, arrays and list

php
<?php

$string = 'This is a string';
$concat = "This is a string with a $string";
$inner_single = "This is a string with a 'single quote'";
$empty_string = '';

echo strlen('Hello world!');
echo str_word_count('Hello world!');
echo strpos('Hello world!', 'world');

echo 'Hello world!';
echo 'First character of this string is: ' . $string[0];
echo 'Multiple strings' . ' concatenated' . ' together';
echo 'Multiple echo statements', ' concatenated', ' together';

echo 'A string
that spans
multiple lines';

$array = array(
	'key' => 'value',
	'key2' => 'value2',
	'key3' => 'value3',
);

$nested_array = array(
	'key' => 'value',
	'key2' => 'value2',
	'key3' => array(
		'key' => 'value',
		'key2' => 'value2',
		'key3' => 'value3',
	),
);

list('key' => $key, 'key2' => $key2, 'key3' => $key3) = $array;

$object->callMethod('string', 'string2', 'string3');
  • Examples of incorrect code for this rule using default options

Echo, variables, function calls, arrays and list

php
<?php

$string = "This is a string";
$concat = "This is a string concatenated with" . "another string";
$empty_string = "";

echo strlen("Hello world!");
echo str_word_count("Hello world!");
echo strpos("Hello world!", "world");

echo "Hello world!";
echo "First character of this string is: " . $string[0];
echo "Multiple strings" . " concatenated" . " together";
echo "Multiple echo statements", " concatenated", " together";

echo "A string
that spans
multiple lines";

$array = array(
	"key" => "value",
	"key2" => "value2",
	"key3" => "value3",
);

$nested_array = array(
	"key" => "value",
	"key2" => "value2",
	"key3" => array(
		"key" => "value",
		"key2" => "value2",
		"key3" => "value3",
	),
);

list("key" => $key, "key2" => $key2, "key3" => $key3) = $array;

$object->callMethod("string", "string2", "string3");

Released under the MIT License.