What is the PHP echo function and how to use it? | CodingSource

What is the PHP echo function and how to use it?

Echo or print commands can be used to output to the screen with PHP. Information about the echo command will be given here. PHP is a server-side scripting language compatible with HTML. PHP is not compiled by a compiler like HTML is, it is only interpreted by the php program on the server.

You can print more than one parameter to the screen with the echo command. However, the print command is suitable for a single parameter. Also, while it returns a value like a function in the print command, there is no return value in the echo command.

Using the Echo Command

If a text is to be printed to the screen with the echo command, as in the example below, the text is written to the screen between double quotes or single quotes.

<?php

echo "Hello"; // Writes Hello to the screen

echo 'How are you'; // How are you written on the screen

?>

Print ve printf şeklinde de ekrana yazdırabilirsiniz. 

<?php

  printf(“This is a PHP file.<br>\n”);

  print(“This is a PHP file.<br>\n”);

  echo (“This is a PHP file..<br>\n”);

  echo “This is a PHP file.<br>\n”;

?>

The codes you have written have been translated into normal HTML codes by the php program in the server system and the visitor cannot see any content other than these codes.

If you have noticed, the code in the source of our page is written in four lines, if we removed the "\n" character after each text in our PHP program, the commands in the source of our page would be placed on a single line.

However, since this does not change the part of the page that is visible to the visitor, it is not an issue that should be emphasized much.

Another issue is the <br> tag that we use in our codes. Yes, it will be very convenient for us to be able to use HTML tags directly in PHP. PHP files are stored on the server system with the .php or .php3 extension (.php3 is used for PHP Version 3). <?php ?> or <? to show where we are using PHP codes in our file. We use ?> tag ranges. From where these tags are used, the server sends the commands in the tag range to the php interpreter, and the php interpreter converts these codes to plain HTML codes.

Printing with variable value

If the value of a variable is to be printed on the screen, you can do this as in the example. However, if you pay attention to the use of echo '$message' here, instead of writing the value of the variable to the screen, it will print what is written in quotes as it is. Variables written in double quotes are detected, while those written in single quotes are perceived as text (string).

<?php

$message="Hello";

echo "$message"; // Writes Hello to the screen

?>

You can use html tags (tags) while printing a value to the screen in Echo. When html tags are used, you will see the result of the html tag on the screen. For example, the following example uses the <br> tag. In this example, <br> will not appear on the screen, but <br>'s task will be to move to a lower line.

<?php

echo "Hello<br>"; // Writes Hello to the screen and moves to the next line

echo 'Hello<br><br>'; // Writes Hello to the screen and moves to the two bottom lines

echo '<br>Hello'; // Moves to the next line and writes Hello to the Screen

?>

A comma (,) separator can be used to print more than one value to the screen with the echo command.

<?php

$name="codingsource";

$age=32;

echo $name," ",$age;

?>

You can do a similar spelling using a period (.). The dot is also used to combine consecutively entered values.

<?php

$name="codingsource";

$age=32;

echo $name." ".$year;

?>

With PHP, you can perform not only textual operations, but also mathematical operations. As in the C programming language, the symbols + for addition, - for subtraction, * for multiplication, / for division and % for finding remainder are used for this purpose.

If you want, let's introduce mathematical operations with a few examples.

<?php

###########################

#   Mathematical operations #

# mat.php #

###########################

# Defining variables

$a = 10;

$b = 2;

$c = 3;

# Defining the mathematical functions to be used

$total = ($a + $b + $c);

$carpim = ($a * $b * $c);

$section = ($a / $b);

$remainder = ($a % $c);

# Outputs are printed to the screen

echo "Sum of defined variables: <b>$total</b><br>\n";

echo "Product of defined variables: <b>$carpim</b><br>\n";

echo "Part by division of first and second variable: <b>$section</b><br>\n";

echo "Remainder after division of first and third variable: <b>$remainder</b>";

?>

The above example may be a bit confusing for someone who has not dealt with programming before. First, values ​​are assigned to the variables and a different result is obtained by using certain functions and passing these values ​​through the desired operations. Just like functions used in mathematics, certain operations are applied to the desired variable value.

So, like y = f(x, w, z) = x + w + z, we used a variable called $sum instead of y and variables like $a, $b and $c instead of x, w and z. The only difference from the functions used in mathematics is that the types of variables used are different and operations can be performed other than mathematical operations.

In our example, the sum, product and division of some mathematical values ​​are taken. It is possible to extend and complicate these mathematical operations in line with the desired purpose.

HTML source code of the PHP file we wrote

Sum of defined variables: <b>15</b><br>

Multiplication of defined variables: <b>60</b><br>

Division result of division of first and second variable: <b>5</b><br>

Remainder after dividing the first and third variables: <b>1</b>

It will consist of plain HTML codes such as The visitor has no chance to see our PHP codes in any way. The \n character we used from the source code made the lines in our HTML output not as a single line, but as lines written one after the other. While its use is not necessary in such cases, there are certainly cases where its use is necessary.

If you have carefully examined the previous example, I used " (quotation mark) while giving the value of the variable there, but I did not use it in the last example. When quotation marks are not used, PHP will understand that our variable is a numeric variable.

Series

Arrays are elements that are sometimes unavoidable and every PHP programmer should know how to use. As the name suggests, arrays store multiple values ​​within the value of a single variable. For example, an array of variables containing the days of the week.

  $week = array(“Monday”, ”Tuesday”, ”Wednesday”, ”Thursday”, ”Friday”, ”Saturday”);

can be defined as. If you notice, "Sunday", the last day of the week, is not included in the series. To include Pazar in the array

  $week[] = ”Sunday”;

We can use an expression like Thus, the last specified "Sunday" element becomes the last element of the array.

To write an element in the array to the screen, for example the 3rd element of the array,

  echo $week[2];

command is used. Why did we use the number "2" for the 3rd element of the array? This is because the first element of the array is the 0th element, not the 1st element. The number of elements of an array variable can be found with the count() function.

  echo count($week);

will print the number 7 on the screen. You can find information about array variables and the functions that operate on them in the "Array Functions" section of the PHP Manual.

Logical Operators

The logical operators that make up the expressions you will use most in a PHP file may be a little difficult to understand at first for a person who has not received programming logic before, but I took this situation into consideration while explaining this section and tried to use a simple and understandable language as much as possible.

At this stage, I would like to remind you again that the logical operators used in PHP are exactly the same as those used in C language, and there is no difference in their usage. I found it more appropriate to tell which operators are used first and then how they are used.

Operator Name

&& And

|| Or

== Equals

! Not

!= not equal

> Greater

< is less

>= Greater than or equal to

<= less than or equal to

We will use the above operators to compare logical expressions and test the correctness of the given condition. Except for the first two operators, someone with some mathematical knowledge has guessed how the other operators are used. In fact, the first two are part of the logic that has been covered in mathematics.

The information coming to the expressions we will use is evaluated in two ways in computer language. 0 and 1, that is, false and true. All the expressions we will construct are limited around these two values.

&& Operator (And) || Operator (Or)

0 && 0 = 0 0 || 0 = 0

1 && 0 = 0 1 || 0 = 1

0 && 1 = 0 0 || 1 = 1

1 && 1 = 1 1 || 1 = 1

Above && and || I wrote the values ​​that the operators create in line with the values ​​they take. We will use these comparison operators in logical expressions, which I will explain shortly.

PHP Codes Related Posts

Newer Post Load More Pages..