like in every modern programming language it is possible to store values of nearly every type inside so called variables (Like in mathematics x,y,z for equations). In the course of this tutorials you will progress in using variables. Today I will only tell you how they work, how they behave in certain circumstances and I will also write something about typification (which sadly doesn't exist in PHP as like in other programming languages like c# or Java)
Fundamentals about variables
Variables in PHP are formed through a $ sign followed by the variable name. For the variable name you have to differentiate large and lowercase letters.
F.e.:
$thisIsAVariableName
Variables always have to begin with a letter from a-z, A-Z or a _. Starting a variable name with a number is not allowed.
$var = "You";
$vaR = "and";
$Var = "I";
$vAr = "will learn PHP";
echo "$var $vaR $Var, $vAr"; // will show "You and I, will learn PHP"
Here are some further examples to differentiate between valid and invalid variable names:
$2steem = 'I use an invalid variable name';
$_2steem = 'this is valid';
$möhrchen = 'I use an invalid variable name';
So you see. The first character in the name is essential. Also inside the name there are some characters that are not allowed. (The ö in möhrchen)
As a regular expression a variable name should look like this:
'^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'.
[a-zA-Z_\x7f-\xff] defines the frst letter of the regular expression. As i said, you are only allowed to begin a variable name with the letters a-z, A-Z, _ or an ASCII character from 127 to 255 (0x7f to 0xff)
[a-zA-Z0-9_\x7f-\xff] defines nearly exactly the same except you are allowed to use numbers inside the variable name.
Assigning values
In PHP, there is no need to set a type to your variables. In PHP a variable exists as soon as you use it. In this process a type will be given to this variable automaticaly depending on the value given to the variable.
Some examples:
<?php
$string = "I am a String!";
$int = 10; //This is an integer
$float = 1.23456 //this is a float (floating point number)
$array = ["This", "is", "an", "array"]; // an array can also hold mixed types. Even arrays in arrays are possible. But I get to this in the next tutorial
Variables can also be instances of objects. It would look like this:
$instanceOfObject = new SomeClassToInstantiate();
I just wanted you to see this. I will have some tutorials in this matter later on.
Usages of variables
So now you know there are different types. And you kind of know how you can output your variables. But there is also a possibility to calculate with your variables.
F.e.:
$x = 4;
$y = 5;
$z = $x + $y;
echo $z;
This will output "9"
You can also use variables in between texts, in, for example, dynamic outputs.
Like so:
$username = "smooms"; // would be fetched from a database later on. ;)
echo "Welcome $username";
// or like this...
echo 'Welcome ' . $username;
That's the next point. There is a difference on using " or ' for strings in PHP. When you know you only want to store text inside a variable, then you should use '.
When " is used, everything in this variable would be parsed. So using " would use more computing power than '.
Some shortcomings
Like I said in the beginning, there is no such thing as directly assigning a type to your variables in PHP. In C# for example you have to declare that the variable "number" is from type integer.
Like so:
int number = 5;
It's not needed in PHP. Also in PHP, since you have declared a variable in PHP yu can feed it with every value you want. Once from type string, in the next row you can feed it with an integer and it is not longer a string.
Like so:
$aStringInReality = "Hello World!";
echo $aStringInReality;
$aStringInReality = 5;
echo $aStringInReality;
The output will be "Hello World!5
Java or C# would throw Type missmatch errors and stuff. Not PHP.
Homework
If you choose to do them of course.
You should get yourself an IDE with PHP highlighting for further usage hence it will enhance your coding speed a lot. And you have autocompletion as well. Nice to have. You can download Eclipse for free. Make sure you download Eclipse with PHP extensions. Otherwhise you are only able to code highlight Java. That's what you don't need at the moment.
If there is some need on a tutorial on how to get your hands on Eclipse and how to set things up with Eclipse, then feel free to say so in the replies.
Once installed you can try some things out with things you have learned so far. You can tell what you did in the replies as well.
Next time
The next time I will tell you something about if statements, loops and eventually a little bit more about arrays.
If you are new with my tutorials and don't know how to execute PHP code, then I encourage you to read this.
And of course feel free to follow @smooms for more and I appreciate every upvote and resteem to help other people learn to code PHP.
Since then and have a nice week.
@OriginalWorks @cleverbot @banjo @steemprice BTC @steem-untalented
$15906.0 USD/BTC
Volume: 224132.45 BTC ($3565050533.59 USD)
Last Updated Fri Dec 8 23:00:31 2017
To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!
I guess you have had more productive conversation, but that is not why I talked to you.
Resteemed upvoted
Hey @smooms it's great to see you have started this!
I don't currently have lots of time to follow along with the tutorials because it is coming up to finals week at my uni. But I plan to follow this series when I have more time over Christmas!
Cheers :)
That's nice to read. :) Well I will hopefully not disappear. I was looking forward to do this tutorials weekly so you have something to catch up then. I keep everything as simple as possible so this shouldn't be a problem then.
click here! , TeamMorocco! .This post received a 7% upvote from @morwhale team thanks to @honolulu! For more information,
Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.Congratulations! This post has been upvoted from the communal account, @minnowsupport, by Smooms from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the
If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP. Be sure to leave at least 50SP undelegated on your account.
Your post has been resteemed to my 2500 followers
Upvote this comment if you like this service