Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] 6 Cool PHP Tricks You May Not Know

Newbie Spellweaver
Joined
Aug 6, 2013
Messages
38
Reaction score
30
Well, It has been a few days short of a year since I posted an HTML version on my old account and now I figure for a yearly present, I shall share a PHP version.

---------------------------------------------------
1. Count Characters in a String

To do this, I've usually just used the function strlen(). However, there is a faster method. Take a look at the following code example:

<?php$string = 'testing';if(isset($string[6])) echo "The string '$string' is at least 7 characters long.";else echo "The string '$string' is less than 7 characters long.";You treat the $string value like an array by passing an integer value to isset().

If that number plus one is greater than or equal to the number of characters in the string, it will return true. You need to add one because it is zero based.

2. Use PHP Echo like a Function
I've always thought that if you wanted to concatenate strings with echo, you needed to use periods. But you can actually treat echo like a function and use commas instead (it is also faster). Take a look at the following code:

<?php$string1 = 'test-string1';$string2 = 'test-string2';$string3 = 'test-string3';echo 'String #1: ', $string1, '<br />';echo 'String #2: ', $string2, '<br />';echo 'String #3: ', $string3, '<br />';

3. Use Single Quotes When Possible


By using single quotes instead of double quotes, you save PHP from having to parse your string for variables. It not only is faster, but I find it more programmer-friendly because it is easier to find variables in your code.
Also, when referencing an array that has a string index, always use single quotes. This prevents PHP from having to figure out exactly what you were trying to say.

4. PHP Variable Variables

There have been several cases when I needed to access a dynamic variable (where the variable name changed). You can do this easily in PHP by using what is called Variable Variables. Take a look at this example:

<?php$var1 = 'nameOfVariable';$nameOfVariable = 'This is the value I want!!!';echo $$var1;

5. Use Arrays in Form Fields


Not only can you create a form field that creates an element in an array (likename['firstname'] ), but you can create dynamic arrays as well. This is especially useful in checkboxes, where the user could check multiple options. Take a look at this HTML:

<label><input type="checkbox" name="hobbies[]" value="Sports" /> Sports</label><br /><label><input type="checkbox" name="hobbies[]" value="Hiking" /> Hiking</label><br /><label><input type="checkbox" name="hobbies[]" value="Swimming" /> Swimming</label><br /><label><input type="checkbox" name="hobbies[]" value="Swimming" /> Watching Movies</label><br />

When the above fields are posted to a PHP page, each hobby is added to the hobbies array.
You can then loop through that array and access each value that was checked.

6. PHP Output Buffering

I have come across cases where something was being output to the screen that I didn't want (at least at that point, or maybe not at all).
A common example of this is if you have a function or a script that echoes out a string, but you are using the code in that instance where you don't want it to print to the screen at that point (when using a template system or framework system, for example). In this case you would still want to be able to reuse the code, but just prevent anything from being printed out at that exact point.
This will make more sense if you look at this simple example:

<?phpob_start();echo 'Print to the screen!!!';$getContent = ob_get_contents();ob_end_clean();// Do whatever you want...// Do something with the printed content (only if you want)...echo 'Now: ' . $

---------------------------------------------------

I hope you have taken something useful out of this article, A big thanks to the people at:


For this.
~ Stay Classy, Ragezone...
 
Joined
Jun 23, 2010
Messages
2,323
Reaction score
2,195
Not something new for me but there are some good points in it. And a note: to access the posted array from a form, use $_REQUEST to access it. $_POST wont work.

Verstuurd van mijn HTC Butterfly
 
Joined
Dec 20, 2010
Messages
418
Reaction score
605
Basic PHP knowledge, but still good nonetheless. 1 cool BB Code trick for you, use the PHP code tags instead of a green font, and add some enters so the variables don't hook up to the <?php etc. Makes it better to understand.
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
Not something new for me but there are some good points in it. And a note: to access the posted array from a form, use $_REQUEST to access it. $_POST wont work.

Verstuurd van mijn HTC Butterfly

$_POST will work just fine if the form method was set to post.
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
Just checked it out and I was wrong. I was sure it didn't work, but its a year ago since I used it. Maybe it got fixed? :p

I honestly doubt that since it's been over a year that I've used forms and arrays in PHP, I'm sure that it worked for me 3 years ago. You probably made a minor mistake on the form or PHP backend.

All sorted now though, seems like this thread has value after all.
 
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
Here's my 2 cents:

1. Count Characters in a String

To do this, I've usually just used the function strlen(). However, there is a faster method. Take a look at the following code example:

<?php$string = 'testing';if(isset($string[6])) echo "The string '$string' is at least 7 characters long.";else echo "The string '$string' is less than 7 characters long.";You treat the $string value like an array by passing an integer value to isset().

If that number plus one is greater than or equal to the number of characters in the string, it will return true. You need to add one because it is zero based.

You can't count characters in a string using isset. You can verify that there's a character at a given offset, but that doesn't make it the same as using strlen. That's the same as comparing isset to count().

2. Use PHP Echo like a Function
I've always thought that if you wanted to concatenate strings with echo, you needed to use periods. But you can actually treat echo like a function and use commas instead (it is also faster). Take a look at the following code:

<?php$string1 = 'test-string1';$string2 = 'test-string2';$string3 = 'test-string3';echo 'String #1: ', $string1, '<br />';echo 'String #2: ', $string2, '<br />';echo 'String #3: ', $string3, '<br />';

is still a better way of doing that.

3. Use Single Quotes When Possible

By using single quotes instead of double quotes, you save PHP from having to parse your string for variables. It not only is faster, but I find it more programmer-friendly because it is easier to find variables in your code.
Also, when referencing an array that has a string index, always use single quotes. This prevents PHP from having to figure out exactly what you were trying to say.

If I'm not mistaken then PHP scans for variables before parsing the call to echo, so that's a minor difference. I'd prefer double quotes because they refer to a string whereas single quotes refer to an index.
 
Elite Diviner
Joined
Jul 25, 2013
Messages
466
Reaction score
55
Amazing tutorial. Thanks! I will practice them regularly to be used to them. (I'm trying to learn PHP).
 
Newbie Spellweaver
Joined
Aug 14, 2013
Messages
5
Reaction score
1
Thanks !
I will begin to try, learn and use them as soon as posible.
 
Experienced Elementalist
Joined
Jul 23, 2012
Messages
201
Reaction score
128
---------------------------------------------------
1. Count Characters in a String

To do this, I've usually just used the function strlen(). However, there is a faster method. Take a look at the following code example:

<?php$string = 'testing';if(isset($string[6])) echo "The string '$string' is at least 7 characters long.";else echo "The string '$string' is less than 7 characters long.";You treat the $string value like an array by passing an integer value to isset().

If that number plus one is greater than or equal to the number of characters in the string, it will return true. You need to add one because it is zero based.



note:
isset($string[x]) will be faster only if used directly, if you wrap it into function like

function strlen_($s,$c) { return isset($s[$c]) } it will be slower, because php strlen_ function will be looking in hash table... anyway these kind of micro-opts are not needed...
 
something
Joined
Dec 26, 2007
Messages
423
Reaction score
12
The day I learned about output-buffering, was one of the greatest days in my life. I can still remember the day :sniff:
 
Back
Top