
The reason behind these dollars signs is simple and might save you some forehead wrinkles! Lets look at my gmail function. My goal was to make my program send an email to whatever input a user would enter. However I couldn't simply use a variable called "email" because of one computer science principle: global and local variables. Within functions, such as our email function, variables are local to the function or to say only exist within the function. This also means, I can't call upon another variable to use in the function. Now this is easily fixable! By adding a "$" to our variable name, the variable become global. This means that these variables permeate the script and can be used anywhere! So by adding $ to "email," I can ask a question that receives input outside the function and have the input used within the function! However, be aware variables "email" and "$email" are not equal! If these two are defined within a program, they will have two separate values.
No comments:
Post a Comment