I Hate Quick Questions

Computer related issues and really random ones

  • Bootstrap
  • Tools
    • WordPress Minify Content
  • iPhone
  • Jailbreak
    • Evasi0n
  • PhP
  • Slow Computer
  • Storage
  • PC to Mac
  • Fix UTF8 Converter

How To Define A String The Right Way With PHP

October 16, 2013 / 1 Comment

string

So if you are reading this and the first thing you are thinking of is, “What’s the definition for the word string?” then you are in the wrong place and you should go now. Not because I think you are dumb, but more that you are going to fall asleep at what I am going to say so you might as well go.

Now the rest of you that are here then lets dive into this a little deeper. If you code in any manner pretty much you are going to understand what I mean by “string”, but not every coder truly understands how to use them in PHP. Now you can’t really go wrong in how you decide to use them, but you can make life easier. Trust me as a coder I see so many lines everyday that shaving off a few seconds here and there can make a huge difference.

First of all there are 4 ways to define a string.

  • Single quoted strings?will display things almost completely “as is.” Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash?\', and to display a back slash, you can escape it with another backslash?\\?(So yes, even single quoted strings are parsed).
  • Double quote strings?will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that?you can use curly braces to isolate the name of the variable you want evaluated. For example let’s say you have the variable?$type?and you what to?echo "The $types are"?That will look for the variable?$types. To get around this use?echo "The {$type}s are"?You can put the left brace before or after the dollar sign. Take a look at?string parsing?to see how to use array variables and such.
  • Heredoc?string syntax works like double quoted strings. It starts with?<<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don’t need to escape quotes in this syntax.
  • Nowdoc?(since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<‘EOT’.?No parsing is done in nowdoc.

Don’t worry you are pretty much only going to most of the time use the top two ways. Single and Double Quotes. But here is where many coders do not know how they work. On easy way to tell them apart is remember that single quotes are ‘as is’ so what you see is what you get. Here is an example

‘This is a sentence with a single quote’

Now lets add a variable to the mix

echo 'This is a sentence with a single $quote';

would out put as

This is a sentence with a single $quote

Remember that single quotes are as is, so what you see is what you get. Variables placed in single quotes display as text. If you want to have a variable display in your string use double quotes.

So

$quotes = 'Winner';
echo "This is a sentence in double $quotes";

Would out put as

This is a sentence in double Winner

See 🙂

Related Posts

  • Words with friends cheat – How to cheat your way to the topWords with friends cheat – How to cheat your way to the top
  • How To Capitalize Categories In WordPress With php/mySQLHow To Capitalize Categories In WordPress With php/mySQL
  • Getting Google Analytics Demographic Data To Work With WordPressGetting Google Analytics Demographic Data To Work With WordPress
  • Why You Should Turn Off Read Receipt For Your iPhoneWhy You Should Turn Off Read Receipt For Your iPhone
  • Heartbleed Bug Hacks Android Phones: Should Have Bought An iPhoneHeartbleed Bug Hacks Android Phones: Should Have Bought An iPhone
  • The Coolest Apps I Use Right Now To Help My Life With SEOThe Coolest Apps I Use Right Now To Help My Life With SEO
  • Why In The Hell Is My Computer So Slow?Why In The Hell Is My Computer So Slow?
  • Storage Almost Full and Vine for iPhoneStorage Almost Full and Vine for iPhone
Posted in: Everything, PhP Tagged: PhP, String

Some fun side project sites

Scrabble Dictionary – Fun site I made as a joke for a friend.

Seamlessdesk – Help Desk Software I developed

Fix UTF8 Online Converter

Stalk Me

Recent Posts

  • MacOS Mojave installer keeps saying the file is damaged or corrupted, HELP!
  • proftpd keeps crashing or just stops working and I don’t know why
  • How to “For Realz” remotely install profile manager profiles silently with no MDM using Apple Remote Desktop
  • Trade Skill Master – Determining Market Value Region vs. Min BuyOut
  • Upgrade any Macbook Flash Drive even when The Apple Store says You Cant

Copyright © 2022 I Hate Quick Questions.

Me WordPress Theme by themehall.com

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok