Printing data and variables in PHP - ProgrammerTech
Language:

Search

Printing data and variables in PHP

  • Share this:
Printing data and variables in PHP

echo print function

The echo function in php is similar to the print function in Python or the printf function in C. It can print anything like (numbers - texts...etc) and since php is a bit tricky compared to html we will create examples and explain them. The echo function can be used with or without parentheses. It can be used in the following two forms: echo or () echo. The echo function can output and print one or more text separated by a comma.

 

Examples of the echo function for printing

example 1

We will run the server to be ready to work, then we will create a file inside the web folder that was created in the previous lesson inside the main www folder and we will create our first example.

<?php
    echo ("programmer tech");
    echo "programmer tech";
?>

In this example, the function will print the sentence inside the echo because it is the print function, and it can also print the sentence with or without parentheses.

example 2
<?php
    echo "<div style='color:red;'>programmer-tech</div>";
?>

In this example, we have added html and css codes to format the stamp that will be output from php because this language can add html and css codes.

The output will be inside the server like this

programmer-tech

Note that we replaced the first and second quotation marks with this form " ' ' " and this is in order not to cause any problems and we will talk about this in the next lessons.

 

example 3
<?php
    echo "kaissar" . "data";
?>

In the previous example, we printed kaissar .data and this sign (.) can be used as an alternative to print each one on a line.

This helps to reduce the size of the code and the output will be:

kaissardata

Note that we used this tag after each line (;) to tell php that we have finished this line and without it the program will not run.

 

variables in PHP

Variables ( variable ), as we know that a variable is a “box” place to store specific data inside the script or code. And studying variables is the most important part of the php language, which is a name that is created and data is stored within this name, and then we can display that data and determine what we want to do with this data. It can contain text, real numbers, a decimal number, or an operation inside the script that can be saved inside that variable...etc.

 

Declaration on variables

It differs in programming languages ​​to declare a variable as an example in javascript to declare a variable you must use the reserved word var. But to declare the variable in php, this sign must be used $ or what is called a dollar.

<?php
$text = 'ahmed kaissar';
echo $text;
?>

When we run this code in the browser we will get the result

ahmed kaissar

Example $name: We created a variable named name, but that variable does not save anything in it, let's learn about the things that can be saved inside variables.

<?php
name;    /Not assigning a value to the variable
name1 = 10;    /Assign a value to the variable 
name2 = 3.5;    /Decimal number
name3 = null;    /null value
name4 = false;    /Boolean
name5 = $name1;    /Assign a variable to a variable
name6 = "welcome";    /text string
name7 = 'kaissar';    /Single-quote string
?>

In the code in front of you all the data that the variable saves.

 

The arithmetic operations that the variable memorizes

  1. For multiplication we use the sign *.
  2. The division operation we use the sign /.
  3. In addition, we use the + sign.
  4. Subtraction we use the sign -.
  5. The remainder operation uses the % sign.

 

Examples of arithmetic operations

<?php
name = 10;    
name1 = 10.22;
name2 = $name1 * $name;    /multiply two variables
name3 = $name1 / $name;    /Dividing
name4 = $name1 % $name;    /remainder of the division
name5 = $name1 + $name;    /plural
name6 = $name1 - $name;    /Subtract
?>

In the previous code, we learned all that can be done with the arithmetic operations of the variable, now let's see the output on the server.

echo '$name1 + $name'. ($name1 + $name) . '<br>'; /print collection process

As we know that the first variable $name holds the value 10 and the second variable name1 carries the value 10.22 and the result will be in the server.

$name1 + $name 20.22

We also created a text string with the names of the variables, and we printed the grouped value between the two variables, and the result inside the browser was 20.22, which is the value of the first and second variables after addition.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com