Page 1 of 1
Scope?
Posted: Fri Jun 07, 2002 4:32 am
by 9902468
Don't know the right word in english but i think it is scope..
If I code like this:
<?php
$a=1;
?>
Oridinary html a lot and a bit more.
.
.
.
.
<?php
if($a == 1){
.........
will that variable be kept after closing that first php tag?
I'm coding a site that uses an index.php as central file that calls actual content pages and now I need to know how php hanldles variables? Will that work? I read that variables will be kept as long as the script is being executed. What is the definition of one script? What about includes and includes that come from other includes?
Posted: Fri Jun 07, 2002 4:47 am
by twigletmac
Yes in that example you will be able to access $a throughout the page no matter how many opening or closing PHP tags you have (unless of course you
unset it). Once you've included a file you'll have access to its variables and functions as well.
The script is effectively everything in the *.php file and it won't finish executing until it reaches the end of the file or is stopped with an
exit() or die() statement in the code.
Hope this helps,
Mac
Posted: Fri Jun 07, 2002 5:30 am
by 9902468
That pretty much cleared any doubt that I had left about scopes.
And now on to arrays! Tried to look a function that would copy array to array but I couldn't find one... Is there any easy way to do it? Situtiation is this: I must copy many arrays into one big, and IF there are values with same keys, the one that is copied later must replace the earlier one.
I know how to check for dublicates, but what about copy? Sample code will be higly appreciated.
(Both arrays must be with string keys like $array->a_name_for_key )
Posted: Fri Jun 07, 2002 5:36 am
by twigletmac
The best place to start is always the
manual where you can find information about all of PHP's
array manipulation functions including a function called
array_merge() which I think is what you're looking for.
Hope this helps,
Mac
Posted: Fri Jun 07, 2002 5:49 am
by volka
mostly everything is assigned by value in php by default.
Code: Select all
<php
$a=array(1,2,3,4);
$b = $a;
unset($aї1]);
print_r($b) // still a $bї1] (=2)
$bї4] = 'Z';
print_r($a + $b); // arrays concatenated
print_r($b + $a); // be aware of the order
// the second array will not override the first's values
?>
Posted: Fri Jun 07, 2002 6:02 am
by 9902468
Thank you Mac! (And Volka)
I really should have spotted that function, but I'm getting all sleepy, I think it is time for me to quit today; I'm talking all restless here..
(M _ u _ s _ t _ _ s _ l _ e _ e _ p _ _ f _ o _ r _ _ a _ w _ h _ i _ l _ e _ _ o _ r _ _ I _ _ g _ o _ _ n _ u _ t _ s _ ...)
See ya
-99
Posted: Fri Jun 07, 2002 6:21 am
by volka
ahh, I slept in may
