splitting a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

wow suprisingly (to me) that worked! tell me what the & sign did so i can actually learn. thank you so much thou!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the & sign just asked php to make $newstring2 a reference to $array[$i], instead of a copy (saves some memory, and processing time)

While a reference, if you alter $newstring2 or $array[$i], it'll change the other (basically).. If you know C, it's a pointer.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

thanks a bunch
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

off topic and probebly a stupid question but every time i create then write to a text file the first line is a blank line then the text starts on the second line. iv used the "r" mode to open it and the "r+" mode to write to it and it still goes to the second line no matter what. why is this and how do i get around it happening?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you probably have a carriage return as one of the first characters in the file.. if you use a function like [php_man]trim()[/php_man] on each line then add a carriage return to it (making sure not to write blank lines.. it should help...

ghetto way: just remove the line manually.. :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I didn't know that thing about the ampersand. Useful tip ;-)
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

I love being able to use references for cool stuff but it looks like PHP is phasing them out. :evil: I'm not saying don't learn them but if you have a script you plan on going out to a lot of people, that uses references, it might break. I had to rewrite a lot of code because of this not too long ago.

New versions of PHP have a setting on whether or not to complain about about references, I'm not sure if it's turned on by default or not, but basically it says not to use them in the future. Just so you know...
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Robert Plank wrote:I love being able to use references for cool stuff but it looks like PHP is phasing them out. :evil: I'm not saying don't learn them but if you have a script you plan on going out to a lot of people, that uses references, it might break. I had to rewrite a lot of code because of this not too long ago.

New versions of PHP have a setting on whether or not to complain about about references, I'm not sure if it's turned on by default or not, but basically it says not to use them in the future. Just so you know...
references are definately not being phased out.

in php5 objects are automatically passed by reference.

maybe your talking about :

allow_call_time_pass_reference

if set to on, this lets you specify whether a variable should be passed by reference when calling the function itself.

Code: Select all

// good, not being phased out way that will always work

function add_foo(&$text) { // notice reference is func definition
    $text .= 'foo';
}


$bar = 'bar';

add_foo($bar); // $bar is now == 'barfoo'


//
//
//
// bad way, in php5,  only works if: allow_call_time_pass_reference = On


function add_foo($text) { // notice NO reference is func definition
    $text .= 'foo';
}


$bar = 'bar';

add_foo(&$bar); // notice reference at call_time, bad

so its not that references are bad, its just the way you use them. what feyd did is not affected by this at all, and totally allowed.
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Hey rehfeld, yeah I was talking about allow_call_time_pass_reference in PHP4. Man that just made my day. Other people were having that problem on their servers and not mine, and I thought I was forced to not use references. Now I know that I can... SWEET!!
Post Reply