Printing information onto the screen is great, but we don’t always know exactly what information we want to print. While we’re writing our code. For example, if we want to print the visitor’s name, there’s no possible way of knowing that information before they visit. That’s where variables come in variables, are a way to store information in order to reference it later variables can be used to store numbers names or other important data in HP variables must start with a dollar sign followed by a letter or an underscore then an alphanumeric character string. Here’s an example. You see that we have our opening PHP tags, a dollar sign followed by the wordage, and then an equal sign and some value. In this case, 34, and we have our semicolon. Age is a placeholder for the number that we have stored in it. Which is 34 PHP knows age is 34 because of the equal sign,
which means is assigned to or gets, I’ll usually say, gets when referencing variables, we can then use age as if it’s the number 34, printing it or even doing calculations on it, like adding subtracting or multiplying. If we decide later we want to change it, we can do that. Two variables are easier to keep track of you can change the Them on the Fly. And most importantly, like the opening example. Sometimes,
you don’t know, certain information at runtime there are also special variables called constants,
that are defined a little differently. In this case, you would use a function or a keyword called
Define with parentheses around it in the parenthesis. You will have the name of the constant in quotes. And in this case, it’s version then a comma and the You of that constant. Which in this case is 1 Point 1, we can then treat the constant as if it’s a variable without a dollar sign. A
couple of things to note though are that, constants are case sensitive. So if we were to write the version in lowercase here, it would not work. And the important part. The reason that constants are called constants is that they are immutable or can’t be changed at runtime. So if we try to reassign a new value to the version using either the guest’s symbol or the Define function. Again, neither of those will work. Constants are great. If you have some value that you know, you absolutely don’t want to change throughout the run time. In both cases for variables and constants, there are a set of predefined values that we can’t use. These are known as reserved words or predefined variables. So for example we can’t use the word this in a variable assignment. Similarly, we can’t create a constant called PHP
underscore version or some other constants that you see here in the PHP manual. The last important aspect to know about variables right now is that computers are very specific about how different types of data are represented. Words are represented differently than numbers are. And further whole numbers are represented differently than decimal numbers. In programming languages,
you need to tell the computer if you are giving it a word or a number. Luckily, PHP. Handles. For most of this us, we don’t need to declare the kind of data.
Our variables are. Most of the time, PHP will figure out how we want to use it and then do it for
us. This makes PHP a week. A typed language that said there are a few different ways variables can be represented as integers, which are Whole numbers like 0 1 or 45 floats, which are numbers with decimal points. For example, 1.0 or 3.16 characters are single letters, numbers, or symbols,
It’s worth noting a few things about characters. The first is that numbers with and without quotes are handled differently, you can do calculations with the integer one, but not the character one, which is represented by quotes around the number one. It’s also worth noting that capital letters are considered different from lowercase letters. So, a capital, A, and a lowercase,
a are considered different by PHP. This is why you are able to use upper and lowercase letters in your passwords to make them more secure.
The last two variable types. Here are strings which are a collection of characters such as hello or racer 5. And billions can have only one of two values, true or false. Now there is
one more type of variable and that’s called an array of arrays are collections or sets of any of the previously mentioned variable types. And you’ll learn a lot more about those while integers.
Floats and boolean’s can be written without the use of quotes strings and characters must be placed in quotes. So for example, we have a string with the variable name so we have the name get Joe and we have a variable called first underscore letter. So we have first underscore letter gets
Jay. You’ll notice, I used both single quotes and double quotes in the example above and there are nuanced differences between them. But for assignments, like these, we can use the single and double quotes interchangeably. You may also notice that with the first underscore letter,
there are no space spaces are not allowed in variable names. And often multi-word variables will replace spaces with an underscore variable.
Are extremely important. As part of working with PHP, you will use them for storing and retrieving
data and it will make writing complex programs much easier.
Comments
Post a Comment
If you have any doubts, Please let me know