Welcome Guest!

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.

2 Pages V   1 2 >  
Reply to this topicStart new topic
> Php Tips & Shortcuts, Some common ones you use?
joey
post 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. biggrin.gif

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


--------------------
Go to the top of the page
 
+Quote Post
Marc
post Feb 14 2008, 12:55 PM
Post #2


Squeeze Machine
*****

Posts: 574
Joined: 13-February 08
From: Scotland, UK


QUOTE (joey @ Feb 14 2008, 05:54 PM) *
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. biggrin.gif

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
Go to the top of the page
 
+Quote Post
Linda
post Feb 14 2008, 01:38 PM
Post #3


Co-Founder
Group Icon

Posts: 3,079
Joined: 13-February 08
From: Pink House in USA


That's really cool! Simple tips like that are great!


--------------------
Go to the top of the page
 
+Quote Post
Simon
post Feb 14 2008, 01:45 PM
Post #4


Rapid Squeezer
****

Posts: 356
Joined: 14-February 08
From: Dreamweaver


thats really helpful thanks biggrin.gif

Simon


--------------------
Go to the top of the page
 
+Quote Post
c010depunkk
post Feb 14 2008, 01:55 PM
Post #5


Rapid Squeezer
Group Icon

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
Go to the top of the page
 
+Quote Post
paintingtheweb
post Feb 14 2008, 02:47 PM
Post #6


Squeezing
***

Posts: 76
Joined: 14-February 08
From: Las Vegas, NV


QUOTE (joey @ Feb 14 2008, 09:54 AM) *
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. biggrin.gif

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
Go to the top of the page
 
+Quote Post
joey
post Feb 14 2008, 02:57 PM
Post #7


Fresh Squeezed
**

Posts: 48
Joined: 14-February 08
From: Hillsboro, OR


QUOTE (paintingtheweb @ Feb 14 2008, 11:47 AM) *
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!


--------------------
Go to the top of the page
 
+Quote Post
paintingtheweb
post Feb 14 2008, 03:13 PM
Post #8


Squeezing
***

Posts: 76
Joined: 14-February 08
From: Las Vegas, NV


QUOTE (joey @ Feb 14 2008, 11:57 AM) *
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
Go to the top of the page
 
+Quote Post
Blake121
post Feb 14 2008, 06:01 PM
Post #9


Fresh Squeezed
**

Posts: 15
Joined: 14-February 08
From: Scotland


QUOTE (joey @ Feb 14 2008, 05:54 PM) *
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. biggrin.gif

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 smile.gif

This post has been edited by Blake121: Feb 14 2008, 06:02 PM


--------------------
Go to the top of the page
 
+Quote Post
alex
post Feb 14 2008, 09:10 PM
Post #10


Fresh Squeezed
**

Posts: 19
Joined: 13-February 08
From: In Daniel's Rabbit Pen.


QUOTE (paintingtheweb @ Feb 14 2008, 08:13 PM) *
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.


--------------------
Go to the top of the page
 
+Quote Post
Simon
post Feb 15 2008, 04:16 AM
Post #11


Rapid Squeezer
****

Posts: 356
Joined: 14-February 08
From: Dreamweaver


QUOTE (alex @ Feb 14 2008, 10:10 PM) *
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??


--------------------
Go to the top of the page
 
+Quote Post
unitedcraig
post Feb 15 2008, 05:04 AM
Post #12


Squeeze Machine
Group Icon

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 wink.gif *

This post has been edited by unitedcraig: Feb 15 2008, 05:05 AM


--------------------
Go to the top of the page
 
+Quote Post
Antti
post Feb 15 2008, 05:20 AM
Post #13


Rapid Squeezer
Group Icon

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.


--------------------
Go to the top of the page
 
+Quote Post
Simon
post Feb 15 2008, 06:35 AM
Post #14


Rapid Squeezer
****

Posts: 356
Joined: 14-February 08
From: Dreamweaver


QUOTE (Telos @ Feb 15 2008, 06:20 AM) *
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


--------------------
Go to the top of the page
 
+Quote Post
alex
post Feb 15 2008, 08:35 AM
Post #15


Fresh Squeezed
**

Posts: 19
Joined: 13-February 08
From: In Daniel's Rabbit Pen.


QUOTE (Simon @ Feb 15 2008, 09:16 AM) *
Whats so bad about it??


Backwards compatibility, not all servers have it enabled and <? clashes with the XML header.


--------------------
Go to the top of the page
 
+Quote Post
christopher
post Feb 15 2008, 10:22 PM
Post #16


Squeezing
***

Posts: 54
Joined: 15-February 08
From: Ottawa, Canada


QUOTE (Blake121 @ Feb 14 2008, 06:01 PM) *
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 smile.gif


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
Go to the top of the page
 
+Quote Post
jackfranklin
post Feb 16 2008, 06:38 PM
Post #17


Rapid Squeezer
****

Posts: 128
Joined: 16-February 08


I love Joey's first tip! Nice one biggrin.gif


--------------------
Go to the top of the page
 
+Quote Post
Jason
post Feb 17 2008, 04:52 AM
Post #18


Master of the Universe
Group Icon

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.


--------------------
Go to the top of the page
 
+Quote Post
Monie
post Mar 11 2008, 02:43 AM
Post #19


Squeeze Machine