Scope?

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

Post Reply
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Scope?

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Post 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 )
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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&#1111;1]); 
print_r($b) // still a $b&#1111;1] (=2)

$b&#1111;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
?>
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ahh, I slept in may ;)
Post Reply