WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
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 Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
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
Submit Site
Forex Trading Online forex trading platform

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 : 3787
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 
 

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



How to control the number of decimal places when outputting numbers.
Categories : PHP, Strings, Variables
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, 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
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
clearing variables in php3
Categories : Variables, Arrays, PHP
mysql_escape_string
Categories : PHP, MySQL, Databases, Strings
Allows you to parse a deliniated string and put the individual fields in a SELECT option in a form
Categories : HTML, PHP, Strings
A simple function to prevent undefined $_POST/$_GET/$_SESSION variable errors
Categories : PHP, Variables, Errors and Logging
function textwrap will wrap text to any desired width using <BR>\n as the default line break. Default wrap width is 80 columns.
Categories : Strings, HTML and PHP, PHP
This functions compares the current PHP version with a desired version. Because of the 3 tiered version system, a direct compare of a string to phpversion() will not be accurate.
Categories : PHP Configuration, PHP, Variables
Adding dashes to credit card numbers
Categories : Strings, Credit Cards, PHP
I need a trim function/regexp that will trim all " " from the ends of a string.
Categories : Regexps, PHP, Strings
Customizable encoding and decoding strings with security.
Categories : PHP, Strings, HTML and PHP
Printer friendly pages from anywhere on a website.
Categories : PHP, Strings, Content Management
 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))
{
?&gt;
Hello &lt;? echo $name; ?&gt;, Welcome back...
&lt;?
}

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