Escape Characters

In both types of string:

  '  needs to be  \'
  \  needs to be  \\

In ” ” strings:

  "  needs to be  \"
  \t    tab
  \n    new line
  \r    return

Note, using say ‘\n’ will not create a newline character, it will create the characters \n !!!!! \n only works in a “” string!!

Joining strings

Use the period character ‘.’

You can also use .=

    $body = "Some" . " text";
    $body .= "Some more text";

Is String Empty

	$my_string = trim($my_string);
	if (!empty($my_string))
		//Do something

Length

  $length = strlen($MyString);

‘ vs “

‘ single quotes assigns a string using its exact contents. So if you want to include variables you have to do this:

   echo 'His name is ' . $name . ' and his age is' . $age';

” double quotes assigns a string but if the string contains a variable name the php parser will attempt to evaluate it, so you can use it like this:

   echo "His name is $name and his age is $age";

Variables and arrray values in a string

 echo "His name is $name";
 echo "His name is ${name}y";  //<<Curley braces can be used if you need to force PHP to see a variable name within other characters
 echo "His name is ${MyArray['name']}";   //<<Also allows array values to be included

Echo A Block Of Text

echo "Some

text

blah blah";
Abbreviated way of inserting an echo in HTML

“<?=” can be used in place of “<?php echo”

<?="<p>Hello world</p>";?>

Is the same as:
<?php echo "<p>Hello world</p>";?>

Heredoc string

See here

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *