WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Smart Strings and Echos
Categories : PHP, Strings, Variables Click here to Update Your Picture
Justin French
Date : May 04th 2003
Grade : 4 of 5 (graded 9 times)
Viewed : 4757
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Justin French
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

If you want to write clean, efficient, optimised code, you need to pay attention to how you declare
and work with strings.

QUOTES:

If the string doesn't contain any variables, then just use single quotes:
$greeting = 'Hello world';

If the string includes variables, use double quotes:
$name = 'justin';
$greeting = "Hello $name";

Why? If PHP sees single quotes, it doesn't parse the string for variables, so the execution is a
fraction faster.

VARIABLES INSIDE STRINGS
Most people just write the variable directly into the string:
$greeting = "Hello $name";

Or join the strings and vars with periods:
$greeting = "Hello " . $name;

I prefer to wrap the variable in "curly brackets", which helps PHP readily tell where the variable
starts/ends, which means faster, non-greedy substitution:
$greeting = "Hello {$name}";

In addition to finding it clearer for myself to read, it also enables more complex substitutions
with arrays:
$greeting = "Welcome back {$_COOKIE['name']}";

Or when two variables sit side-by-side:
$dir = "something/foo/bah/";
$file = "xxx.txt";
$path = "{$dir}{$file}";


For more information, see the "Variable Passing" section of this page:
http://www.php.net/manual/en/language.types.string.php



Working with files - return an array of files within a directory
Categories : PHP, Strings, Variables, Filesystem
Working with files - putting file contents to a string / var
Categories : PHP, Filesystem, Variables, Strings
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
How to control the number of decimal places when outputting numbers.
Categories : PHP, Strings, Variables
Display variables when a form is submitted using POST/GET
Categories : PHP, Functions, Variables, Debugging
How to find the name of the current file?
Categories : PHP, Filesystem, Strings
What is the best way to split a string that consists of two bits of data seperated by whitespace?
Categories : Regexps, Strings, PHP
A class to put get and post variables in hidden form elements. Works on scalars, normal arrays, associative arrays.
Categories : Algorithms, Variables, Arrays, PHP, PHP Classes
Customizable encoding and decoding strings with security.
Categories : PHP, Strings, HTML and PHP
Can the word DO be used in arrays?
Categories : Arrays, PHP, Strings
How to pass an array to a function, or how to define a function wich recieves an array.
Categories : Variables, PHP, Arrays
Functions that will format a date in either long or short format from a string.
Categories : Date Time, Strings, PHP
Allows you to parse a deliniated string and put the individual fields in a SELECT option in a form
Categories : HTML, PHP, Strings
Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET
Categories : Algorithms, HTML and PHP, PHP, Variables
columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP
 matthew waygood wrote : 948
Probably the first thing you learn to do when starting PHP. So why tell everyone, because in order to be at this site you would have already done some coding and already know this information. Just like telling someone learning html about the BODY tag.
 
 Justin French wrote : 949
I don`t agree -- the main point of my example was enclosing variables inside {braces}, which can allow more complex combinations of strings and variables, something that I very rarely see being used in any PHP related tutorial or article.

The second point of the example was to point out that "double quotes" should only be used when needed (ie, when including variables inside strings).  Based on the plethora of double quotes I see in tutorials, sample code, articles and even open source projects, it would appear that not everyone knows when to use them, or don`t care.

I may have started off a little too "basic", but there`d be a lot more clean code out there if people knew when to use what, and how much easier {$vars} inside curly braces can make things.

The fact that you already knew doesn`t mean it`s a bad example -- it just means you`re smarter than the average bear.
 
 Sarah King wrote : 950
Thanks Justin. 

It never hurts to be reminded of how to write efficient code. Personally I hadn`t known that about the {} and I`ve been coding php for a couple of years now (part time). I`ve seen it in some examples but not found documentation (easily) to explain why.

The other performance option with Strings is whether it`s also better to have 
if ( isset($name))
{
?>
Hello <? echo $name; ?>, Welcome back...
<?
}

Personally I find it a pain in the butt to work with and my scripts aren`t so big that I need the performance to be quite so efficient. But these are all things that are useful to some.
 
 mick 28 wrote : 951
Thanks for the heads up :-) 
I`ve been using PHP for some time and knew about the quotes but only recently discovered the curly braces after looking at an example from another site that used them, your right they are under used and rarely mentioned yet they will save me some time in future especially when using complex variables.
Thanks once again for sharing.
 
 kenji wrote : 953
Yep, I`ve got to agree. You`ve pointed out something I never knew and will start using. BTW, this isn`t that basic so has a place here. 
 
 PMJ wrote : 954
Thanks for showing us (newbies) the small stuff. I want to do things in the proper manner. Back off Matthew.
 
 Robin North wrote :955
Excellent piece of info for optimisation - thanks