PHP Lessons # 2 | Variables, Constants, Types, and Printing Commands / Functions

Discussion in 'Web Programming' started by hbkyatty, Apr 11, 2018.

  1. hbkyatty

    hbkyatty New Member

    Hi Friends, I'm back in the
    second PHP lesson. I share a lesson with you that I find useful on my blog. I hope it touches us. Our current topics: Variables, Constants, Types and Functions with Print Commands.

    In the meantime I could not find a picture to add. There was no picture anyway.

    Variables and Variable Definition Variables in

    PHP begin with a leading dollar sign ($). Just like music notes start with the left key.

    The name of the variable and the equal sign (=) are added next to it and the value is specified against it.

    • No variable name can begin with a digit. ($ 1NAME WRONG)
    • Variable names are case-sensitive. ($ a and $ A are not the same)
    • Variable names can not contain special characters. (II, - / * ^ etc ...) Only hyphens (_) can be used.
    • Variable values can be assigned to specific types. (Such as String, Boolean, Integer, Float ...)

    $ x = 1;

    $ Variable key
    x Variable name
    = Value Description symbol
    1 Assigned value "1"
    ; In

    this example, I define a variable named x and assign it a value of 1. At this point x equals 1.

    So we have also seen a simple variable definition.

    Or we can do this by adding "has" to it as in JS, but we will still have to add the dollar symbol to the beginning of the variable name.

    Variable Types

    We can assign variables to values based on certain types. We can not assign value according to our mind. These are;

    String: Contains texts, phrases, words, and so on. Accepts all characters. Double quotation marks (") are placed between the symbols.
    Integer: Contains numbers and numbers. The number is entered directly.
    Boolean: Incorrect values for the right half. (TRUE: True, FALSE: False)
    Float: Contains decimal numbers. The decimal value with the number is entered directly.

    A string variable:

    $ string = "Hello World!";

    We wrote 'Hello World' between the quotation marks.

    There is something important here. We can use double quotes or single quotes when defining strings in PHP, but there is a difference between them. When I type a variable name in double quotes, it draws the value of that variable. So that variable works in double quotes. However, variables in single quotes do not work, values are not directly defined as $ variable.


    $ x = 'Sun';
    $ y = "Today $ x";

    If I print this on the screen I will get the output will be;

    "Today the weather is sunny"

    If you notice, I used double quotes in y variable and my variable was valued. Let's try it with a single quotation:

    $ x = 'Sun';
    $ y = 'Today's weather $ x';

    I will buy it on the screen and it will be;

    "Today's weather $ x" As

    you see it. Variables in double quotes work, values are taken. Single quotes do not work and no variable name is taken.

    An integer variable:

    $ integer = 10;

    We assigned a value of 10. Numbers or digits that do not have a leading or trailing quotation mark are integer.

    A boolean variable:

    $ boolean = TRUE;

    TRUE (1) so we assigned the correct value.

    And if it is a float variable:

    $ float = 1.5;

    We assigned a value of 1.5.

    So now we have seen how to create variables and define values into them. Variables are an indispensable part of many coding languages. You need to know them well.


    talked about Constants Variables. On the name "Variable", that is, it can change. And there are those that have not changed, once the value has been assigned again. We call them "fixed".

    We also do certain species in the fixtures, just as they are in the variants. These species are already mentioned above. It is the same as variable types.

    Also, in constants, there is no special key like the variable definition symbol ($). We specify a constant by typing the name of the pole.

    For example, there is a test called "experiment". If you write a direct "trial" without putting any sign on your head, it is a testimony.

    When defining a constant, define (fixed, value); function.

    Now let's define a simple constant.


    define ("test", "Hello World!");

    So there is a temptation in the name of the experiment and the value is the string "Hello World!"

    Screen Printing Functions If the

    last topic of our class today is to print a text on the screen. Or through variables / constants.

    There are 3 print functions in PHP.

    echo DEGER;
    print (value);
    print_r (Value);
    Three of them have different characteristics. Let's take a look at them now.

    Maybe you know Echo from behind. On Windows we were also printing text on cmd with echo. In PHP we can also write this.

    echo "Hello World";

    We printed Hello World on the screen.
    print ();

    Print and Echo have the same function. The main difference of Print from Echo is that it returns TRUE (1) when printing to the screen is successful. Returns TRUE (1) if it is printed on the screen, FALSE (0) if it is not. In short, if the transaction is successful, it is correct and if it can not be successful, it is wrong.

    On Echo it prints directly on the screen. It does not do anything else, it does not return anything if it is not successful, and the process ends there.

    print ("Hello World");

    We printed Hello World on the screen.
    print_r ();

    It would be better if we show this in our next lesson. Normally there is not much difference from the others, but the arrays show with the opening (their keys and their values).

    We need to see the divisions for that and I will show him tomorrow. Shuan is enough for today.

    Using Variables in Functions (Basic)

    Essentially, it is enough to write a variable name to use a variable in a function. I want to print a variable on the screen. We do this:


    $ text = "Hello World!";
    echo $ text;

    Our text variable will be printed on the screen and we will receive Hello World! will be.

    Now let's print it differently:

    $ weather = "Sunny";
    echo 'Today's weather' $ weather;

    Note: As you can see, I distinguish between string and variable by a period (.). Do not forget this, it is very important to separate them.

    I hope it was understandable, it was a quality lesson.
    I thank you all for reading and visiting.


Share This Page