I.T. Discussion Community!
-Collapse +Expand
PHP
Search PHP Group:

Advanced
-Collapse +Expand PHP Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...PHPBeginners Co...   Print This     
Next Random Article Next Random Tidbit || Change Topic Random From All
  From the January 2016 Issue of Prestwood eMag
 
PHP Beginners Corner:
PHP Literals (quote or apostrophe)
 
Posted 11 years ago on 11/4/2008 and updated 1/29/2009
PHP Code Snippet:
 A flashcard from our PHP Flashcards Library
 A code snippet from our PHP Code Snippets Page
 Tags: PHP , Literals

KB101524

General Info: Programming Literals

A value directly written into the source code of a computer program (as opposed to an identifier like a variable or constant). Literals cannot be changed. Common types of literals include string literals, floating point literals, integer literals, and hexidemal literals. Literal strings are usually either quoted (") or use an apostrophe (') which is often referred to as a single quote. Sometimes quotes are inaccurately referred to as double quotes.

Languages Focus: Literals

In addition to understanding whether to use a quote or apostrophe for string literals, you also want to know how to specify and work with other types of literals including floating point literals. Some compilers allow leading and trailing decimals (.1 + .1), while some require a leading or trailing 0 as in (0.1 + 0.1). Also, because floating point literals are difficult for compilers to represent accurately, you need to understand how the compiler handles them and how to use rounding and trimming commands correctly for the nature of the project your are coding.

PHP Literals

In PHP you can use quotes, or apostrophes as in "Prestwood", and 'Prestwood' for string literals. Use a slash in front of a quote or apostrophe to embed same type as in \' and \".

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

Syntax Example:
echo "Mike's drums are over there.<br>";
echo 'Mike said, "hi!"<br>';
  
//Does PHP evaluate this simple
//floating point math correctly? No! 
If ((.1 + .1 + .1) == .3) {
 Echo "Correct";
} Else {
 Echo "Not correct";
}

Working Example

<html>
<head><title>PHP Literals</title></head>
<body>
<?PHP
//
//Quotes
//
echo "Mike's drums are over there.<br>";
echo 'Mike said, "hi!"<br>';
  
//Variable within a literal.
echo "Your name is $fullname.<br>";
echo "You are $Age and weigh $Weight.<br>";
  
//$ within a literal ok too.
echo "That will be $1.52.<br>";
If ((.1 + .1 + .1) == .3) {
 Echo "Correct";
} Else {
 Echo "Not correct";
}
?>
</body>
</html>

More Info

Definition:  Programming Literals

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1103A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101524 Counter
15936
Since 11/4/2008

Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech