Forum Moderators: coopster

Message Too Old, No Replies

always learning in php

I'm always stumbling across simple things that make php coding easier

         

hughie

7:21 pm on Dec 4, 2006 (gmt 0)

10+ Year Member



As the title suggests I always seem to find myself stumbling across little bits and pieces that make life easier.

I'm not a trained programmer but have been coding in php now since 2001 and feel that i'm at a good level and have been able to acheive very complex things in php where once i would have headed for the hills for sit down and an afternoons meditation.

However because of the route I (and many others i'm sure) have taken in coding on-demand, things have been missed out along that way, simple little code snippets that probably would have been taught to me had i gone the book/course approach.

So a little code snippet picked up recently that i haven't stopped using is the & symbol when calling a function.

This basically means that whatever happens to the variables inside the function, these values are automatically applied those variables outside the function that were passed to it.

example


<?php
function change_Namearray($array)
{
$ArraySize=sizeof($array);
for($i=0;$i<$ArraySize;$i++)
{
$array[$i].=' Is Great';
}
}

$names=array('Jim','Sam','Fred');

change_Namearray($names); // NO & SIGN, thus no change
print_r($names);
/*
Prints
Array
(
[0] => Jim
[1] => Sam
[2] => Fred
)
*/

change_Namearray(& $names); // NOTE THE & SIGN
print_r($names);
/*
Prints
Array
(
[0] => Jim Is Great
[1] => Sam Is Great
[2] => Fred Is Great
)
*/
?>

Anyone else got any useful snippets they've stumbled across and gone "darn(or other less polite words), that's useful"?

Hughie

eelixduppy

8:21 pm on Dec 4, 2006 (gmt 0)



More on what you are talking about here: References Explained [us2.php.net]

As for contributing something, well, here's a quote from something I read along time ago, and it helps me make some decisions:


'Write legible, not clever, code.
Break down complex operations into distinct steps, avoid over nesting functions, and name your variables something that make sense.'

This applies to most things. But there are times where it may be necessary, or better, to be clever. ;)

mcibor

10:17 am on Dec 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To say the truth, Hughie it's not a correct code.
It works, but this use is deprecated.

Correct would be

<?php
function change_Namearray(&$array)
{
foreach($array as $key => $value)
{
$array[$key].=' Is Great';
}
}
$names=array('Jim','Sam','Fred');

change_Namearray($names); // NO & SIGN, but changes
print_r($names);

/*
Prints
Array
(
[0] => Jim Is Great
[1] => Sam Is Great
[2] => Fred Is Great
)
*/

For this use you could also use array_map [php.net]:


<?php
function is_great($name) {return $name.' Is Great';}

$names=array('Jim','Sam','Fred');
$changed = array_map("is_great", $names);
print_r($changed);
?>


It's not so good to change vars in a function, because you may get lost what is and what is not changed.
Michal

PS. And use the functions - they are there to be used:
Instead of many ifs and elseifs there is a switch;
Instead of sizeof array and for you can use foreach;

there are so many possibilities...

hughie

8:34 pm on Dec 5, 2006 (gmt 0)

10+ Year Member



hehe, i had a feeling when i posted that someone was going to point out an issue with it, thus is php ;-)

StupidScript

9:04 pm on Dec 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



extract($someArray)
is handy. (Imports the array elements into the current symbol table, eliminating the need to do like
$elem1=$someArray['elem1'];$elem2=$someArray['elem2']...

Also

basename($_SERVER['SCRIPT_NAME'])
returns the current page file name quite nicely without any associated path info.
basename($_SERVER['SCRIPT_NAME'],".php")
will return the file name without its ".php" extension, if that's what it has ...

Just a couple of functions I've learned about, recently ...

henry0

11:37 pm on Dec 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DO NOT OUTPUT/ECHO anything from a function
instead have your function returning true , false (as much as possible)
ex:
function MyFunction($stuff)
if (whatever....)
{
return TRUE;
}
else
{
return FALSE
}
using it
if (MyFunction($stuff)==FALSE)
{
do something
}
etc...