Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
Php Tips & Shortcuts
This is a discussion on Php Tips & Shortcuts, within the PHP section. This forum and the thread "Php Tips & Shortcuts" are both part of the Programming Your Website category.
![]() ![]() |
Feb 14 2008, 12:54 PM
Post
#1
|
|
|
Fresh Squeezed ![]() ![]() Posts: 48 Joined: 14-February 08 From: Hillsboro, OR |
I'm just curious is some PHP short cuts some of you regularly use?
For example, when working with forms I use CODE <? foreach($_POST as $key => $value) { $$key = $value; // the double $$ is intentional. } ?> Replace the POST with GET, REQUEST, SESSION or whatever you're working with as necessary. What this does is pulls all the field names for the form and sets them as simple variables so you can reference the quick variables throughout your code rather than manually defining them or calling them with their long names. $_POST['name'] is now $name ... $_POST['dob'] is now $dob ... etc... ------------ I know my example is nothing amazing, but I use it all the time and there's gotta be someone out there not aware of this technique. Look forward to reading your responses (and I hope this is the right forum for this). Thanks. This post has been edited by joey: Feb 14 2008, 01:45 PM -------------------- |
|
|
Feb 14 2008, 12:55 PM
Post
#2
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Posts: 574 Joined: 13-February 08 From: Scotland, UK |
I'm just curious is some PHP short cuts some of you regularly use? For example, when working with forms I use CODE <? foreach($_POST as $key => $value) { $$key = $value; // the double $$ is intentional. } ?> Replace the POST with GET, REQUEST, SESSION or whatever you're working with as necessary. What this does is pulls all the variables for the form and sets them as variables so you can reference the quick variables throughout your code rather than manually defining them or calling them with their long names. $_POST['name'] is now $name ... $_POST['dob'] is now $dob ... etc... ------------ I know my example is nothing amazing, but I use it all the time and there's gotta be someone out there not aware of this technique. Look forward to reading your responses (and I hope this is the right forum for this). Thanks. Wow! thanks.. I would never of thought of that! -------------------- Thanks,
Marc |
|
|
Feb 14 2008, 01:38 PM
Post
#3
|
|
![]() Co-Founder ![]() Posts: 3,079 Joined: 13-February 08 From: Pink House in USA |
That's really cool! Simple tips like that are great!
-------------------- |
|
|
Feb 14 2008, 01:45 PM
Post
#4
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 356 Joined: 14-February 08 From: Dreamweaver |
thats really helpful thanks
Simon -------------------- |
|
|
Feb 14 2008, 01:55 PM
Post
#5
|
|
![]() Rapid Squeezer ![]() Posts: 197 Joined: 14-February 08 From: Willich, Germany |
One function I use often when working with MySQL if the mysql_fetch_assoc. It returns a result row as an array with the database column names as keys:
CODE $query=mysql_query("SELECT id,name FROM table_name"); $row=mysql_fetch_assoc($query); echo($row['id']); You can also use mysql_fetch_array: CODE $query=mysql_query("SELECT * FROM table_name");
$row=mysql_fetch_array($query,MYSQL_ASSOC); This post has been edited by c010depunkk: Feb 14 2008, 01:58 PM -------------------- www.c010depunkk.com ~ the hangout of a web developer
|
|
|
Feb 14 2008, 02:47 PM
Post
#6
|
|
![]() Squeezing ![]() ![]() ![]() Posts: 76 Joined: 14-February 08 From: Las Vegas, NV |
I'm just curious is some PHP short cuts some of you regularly use? For example, when working with forms I use CODE <? foreach($_POST as $key => $value) { $key = $value; // the double $ is intentional. } ?> Replace the POST with GET, REQUEST, SESSION or whatever you're working with as necessary. What this does is pulls all the field names for the form and sets them as simple variables so you can reference the quick variables throughout your code rather than manually defining them or calling them with their long names. $_POST['name'] is now $name ... $_POST['dob'] is now $dob ... etc... ------------ I know my example is nothing amazing, but I use it all the time and there's gotta be someone out there not aware of this technique. Look forward to reading your responses (and I hope this is the right forum for this). Thanks. What about using extract()? IE. extract($_REQUEST); It does almost the exact thing you're doing. -------------------- http://www.infoonmike.com
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." Martin Golding |
|
|
Feb 14 2008, 02:57 PM
Post
#7
|
|
|
Fresh Squeezed ![]() ![]() Posts: 48 Joined: 14-February 08 From: Hillsboro, OR |
What about using extract()? IE. extract($_REQUEST); It does almost the exact thing you're doing. Hm. I dunno. Will have to give it a go next time. Thanks! -------------------- |
|
|
Feb 14 2008, 03:13 PM
Post
#8
|
|
![]() Squeezing ![]() ![]() ![]() Posts: 76 Joined: 14-February 08 From: Las Vegas, NV |
Hm. I dunno. Will have to give it a go next time. Thanks! No Problem. As far as other shortcuts go, obviously there's the <?=$variable?> as opposed to <? echo($variable); ?>. And as far as SQL goes, has anyone ever used ADODB for PHP? It's quite nice. Clean and easy to use code. -------------------- http://www.infoonmike.com
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." Martin Golding |
|
|
Feb 14 2008, 06:01 PM
Post
#9
|
|
![]() Fresh Squeezed ![]() ![]() Posts: 15 Joined: 14-February 08 From: Scotland |
I'm just curious is some PHP short cuts some of you regularly use? For example, when working with forms I use CODE <? foreach($_POST as $key => $value) { $key = $value; // the double $ is intentional. } ?> Replace the POST with GET, REQUEST, SESSION or whatever you're working with as necessary. What this does is pulls all the field names for the form and sets them as simple variables so you can reference the quick variables throughout your code rather than manually defining them or calling them with their long names. $_POST['name'] is now $name ... $_POST['dob'] is now $dob ... etc... ------------ I know my example is nothing amazing, but I use it all the time and there's gotta be someone out there not aware of this technique. Look forward to reading your responses (and I hope this is the right forum for this). Thanks. There's a neat trick like that, I use CODE //array of expected POST keys $expected = array('key1', 'key2','etc'); //make $_POST data safe foreach ($_POST as $key => $value) { if(in_array($key, $expected)) { ${$key} = mysql_real_escape_string($value); } } You make an array of all the POST keys you are expecting. It then loops through them and turns the expected ones into variables and makes them safe for use in a DB. It then discards all variables that weren't in the array. Makes my forms safer and quicker This post has been edited by Blake121: Feb 14 2008, 06:02 PM -------------------- |
|
|
Feb 14 2008, 09:10 PM
Post
#10
|
|
![]() Fresh Squeezed ![]() ![]() Posts: 19 Joined: 13-February 08 From: In Daniel's Rabbit Pen. |
No Problem. As far as other shortcuts go, obviously there's the <?=$variable?> as opposed to <? echo($variable); ?>. That's bad practice, PHP developers strongly discourage short tags. I always do Object Orientated code so I guess a shortcut would be the reuse feature of classes. -------------------- |
|
|
Feb 15 2008, 04:16 AM
Post
#11
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 356 Joined: 14-February 08 From: Dreamweaver |
That's bad practice, PHP developers strongly discourage short tags. I always do Object Orientated code so I guess a shortcut would be the reuse feature of classes. Whats so bad about it?? -------------------- |
|
|
Feb 15 2008, 05:04 AM
Post
#12
|
|
![]() Squeeze Machine ![]() Posts: 560 Joined: 14-February 08 From: Stockport |
perhaps something with older versions or something, but if alex said there's something bad about it, id believe it lol
*accepts the £20 off alex This post has been edited by unitedcraig: Feb 15 2008, 05:05 AM -------------------- |
|
|
Feb 15 2008, 05:20 AM
Post
#13
|
|
|
Rapid Squeezer ![]() Posts: 307 Joined: 15-February 08 From: Finland |
I'm using ADODB almost in every php project I have to deal with databases. I just love it.
I'm trying to avoid shortcuts since they tend to make the code less readable afterwards and it's not much what I can save time using them. -------------------- |
|
|
Feb 15 2008, 06:35 AM
Post
#14
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 356 Joined: 14-February 08 From: Dreamweaver |
I'm trying to avoid shortcuts since they tend to make the code less readable afterwards and it's not much what I can save time using them. That must be the reason, i never use this short cuts, KISS Simon -------------------- |
|
|
Feb 15 2008, 08:35 AM
Post
#15
|
|
![]() Fresh Squeezed ![]() ![]() Posts: 19 Joined: 13-February 08 From: In Daniel's Rabbit Pen. |
Whats so bad about it?? Backwards compatibility, not all servers have it enabled and <? clashes with the XML header. -------------------- |
|
|
Feb 15 2008, 10:22 PM
Post
#16
|
|
![]() Squeezing ![]() ![]() ![]() Posts: 54 Joined: 15-February 08 From: Ottawa, Canada |
There's a neat trick like that, I use CODE //array of expected POST keys $expected = array('key1', 'key2','etc'); //make $_POST data safe foreach ($_POST as $key => $value) { if(in_array($key, $expected)) { ${$key} = mysql_real_escape_string($value); } } You make an array of all the POST keys you are expecting. It then loops through them and turns the expected ones into variables and makes them safe for use in a DB. It then discards all variables that weren't in the array. Makes my forms safer and quicker Blake121's approach (quoted above) is safer. Blindly creating variables from POST values is like re-implementing the register_globals directive in user code. The register_globals directive was deprecated for a reason: it's a security risk. This post has been edited by christopher: Feb 15 2008, 10:23 PM -------------------- Blog: annoyed.ca | Web Site Hosting: www.bluephyre.com
|
|
|
Feb 16 2008, 06:38 PM
Post
#17
|
|
|
Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 128 Joined: 16-February 08 |
I love Joey's first tip! Nice one
-------------------- |
|
|
Feb 17 2008, 04:52 AM
Post
#18
|
|
|
Master of the Universe ![]() Posts: 1,297 Joined: 15-February 08 From: London, England |
Theres some nice tips here. I already do something similar to what Blake does by checking variables that come into a script.
-------------------- |
|
|
Mar 11 2008, 02:43 AM
Post
#19
|
|
![]() Squeeze Machine ![]() |






Feb 14 2008, 12:54 PM















