PDA

View Full Version : variable syntax



pghcollectibles
04-15-2008, 11:01 AM
most of the time in php a variable looks like $me='confused' for example, in this template im editing i have found many occurances of variables with no $ sign including arrays and stuff.

i have found the page where there might be something like:

define('me', 'confused');

and i see me being used in the php coding the same as a variable with a $ with no quotes.

then there are other variables usually i see them in javascript or something that will be var me='confused' or just me='confused' as you can set and declare at same time, but these variable would look different when using php and im not sure how to tell the difference or change syntax if i wanted to.

so $me = me

??? im confused, can you tell.

i might see echo "hello" . $me;
or a function like who_can_help('me')

and i know that me is a variable being used in php but it doesnt have a $ sign. whats the deal. my main help sites talk about each variable syntax within their own language but i dont see anywhere in php that the variable doesnt have a $ sign and i found things in a search for converting javascript variables to php variables but nothing was that actually did it just other people trying to do the same thing.

the only way i know how to convert into php is by using a form and with the POST GET thing or like
<input type="<?php echo $type; ?>" value="">
which isnt even converting (not that i need to) but just using more than one language to output HTML i guess.

Summary:
what syntax is used for variables with no $ sign in php (quotes, no quotes, etc)
whats the difference between the variable dog and the value dog, for example

for the issue of converting i can go from a php variable into a javascript variable:
<script> dog="<?php echo $dog; ?>"; </script>
but how do you make $dog=dog (the variable)

sorry such a long post

sjlplat
04-15-2008, 06:18 PM
PHP define() is used to create constants, not variables. The main differences between constants and variables is that constant values cannot be changed during the execution of a script, and they are global. Variables can be changed anytime, and variable scope is limited to the executing script file and included or required files.

Read more about PHP define() at http://us.php.net/define

Seb
04-15-2008, 06:21 PM
I think you better start reading the PHP-manual friend. You will find answers to most of your questions if you are just patient and learn to use it.

In PHP the variables without $ are not variables, they are constants (http://se2.php.net/manual/en/function.constant.php). Constants are usually written in uppercase letters so they can be spot easier.

define('MY_CONSTANT', 'this is is not a variable and cannot be changed during runtime');
echo MY_CONSTANT;
Also, variables of objects doesn't have the $ infront of them as seen in the other topic, but it's object has:

$object->variable;

As for the Javascript and variable declarations. It's a best practice to do the var but not always. It has to do with variable scope.

<script type="text/javascript">
function my_function()
{
var test1 = 'hello';
test2 = 'hello again';
}
my_function();

alert(test1); // this variable is not accessible outside the function (undefined)
alert(test2); // this one is and will bring up 'hello again';
</script>

pghcollectibles
04-15-2008, 09:54 PM
ok thank you i will look into constants. i did notice all those in the define code were capitalized. i thought once a variable was declared, it could be used anywhere this is because of the var i guess? i have always only seen the var in front of a variable if anything, but might there be something else placed before the variable for other reasons and what is var? not what it does but what it is ie operator, parameter, object? that kind of what is it.

also any example of $php_variable=java_variable without using POST, GET