2

file name myServices.php

<?php
   $gender = 'MALE';
?>

in another file lets say file.php

    include "myServices.php"

    $name = 'SAM';
    $age  = '23';
?>

<!--after some more HTML code-->
<?php

    $gender = 'FEMALE';        
    $name = 'ELENA';
    //Question:
    //In the above statements are there new variables created or the 
    //previous variables are reassigned new values

?>
1
  • You might want to take the question out of the code block, I had a really hard time finding it. Commented Aug 18, 2010 at 3:12

3 Answers 3

11

The previous variables are reassigned new values.

Sign up to request clarification or add additional context in comments.

Comments

1

If it is just like you have it listed, then the $name and $gender variable's values are replaced with "ELENA" and "FEMALE"

Why don't you try, echo $name;, echo $gender;

Comments

1

As Codeacula said, those variables will be overwritten. The opening and closing PHP tags do not define scope. Variables are in what's called the global scope unless they are inside of a function or class. Global methods are as the name implies available, and can be overwritten inside functions and classes

When a variable is inside of a function then that variable is only available inside of that function unless it is prepended with the keyword global.

A quick search on google will give you more information on variable scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.