[PHP] A little tip I learned today.

Status
Not open for further replies.

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
This is nothing much, and is hardly even a tutorial, but I've recently started a new project and come across something on the Internet about PHP.

It is to do with echoing/printing strings on the screen, usually you would do:

PHP:
<?php
$first = "Mark";
$last = "Eriksson";
echo $first . " " . $last; //output: Mark Eriksson
?>

Well I've now been told to do it like this because apparently it is quite a lot quicker than using periods (.):

PHP:
<?php
$first = "Mark";
$last = "Eriksson";
echo $first , " " , $last; //output: Mark Eriksson
?>

It does exactly the same job, but apparently it is quicker.
 

Sledmore

Chaturbate Livestreamer
Staff member
FindRetros Moderator
Jul 24, 2010
5,194
3,901
Nice tip, cheers for the share, ;]
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,926
3,921
The difference is so minimal that it really doesn't make a difference, but yes, the second one does use less memory.

Why? Because it's just echoing each one after the comma, where as the first one is joining the string together, and then echoing it out.

You can read more about it here:
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
No worries Sledmore!

Rasta - I did actually hear about it on a YouTube video by phpacademy but only just decided to look it up properly, I ignored it really on the video.
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
I was thinking of posting about this, yesterday I read an article and basically, I ended up changing all my work so it is like this.

String with no variables:
PHP:
$str = 'WOOO THIS IS a string';

String with variables:
PHP:
$wee = "lols";
$str = "HEY, DIS IS LYK SUUU FUNNAY. {$wee}";

What I chosed to use:
String without variables, fastest method.
String with variables, second fastest method, I chose since with it you could also include methods and everything would look equal.

Nonetheless, Rasta is right, the difference is so minimal it really doesn't matter.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
I was thinking of posting about this, yesterday I read an article and basically, I ended up changing all my work so it is like this.

String with no variables:
PHP:
$str = 'WOOO THIS IS a string';

String with variables:
PHP:
$wee = "lols";
$str = "HEY, DIS IS LYK SUUU FUNNAY. {$wee}";

What I chosed to use:
String without variables, fastest method.
String with variables, second fastest method, I chose since with it you could also include methods and everything would look equal.

Nonetheless, Rasta is right, the difference is so minimal it really doesn't matter.
Not gonna lie, I don't understand what you mean with your code :p
 
Status
Not open for further replies.

Users who are viewing this thread

Top