PHP syntax Variables Data Types
PHP scripts are written inside the PHP syntax. PHP syntax starts with <?php and ends with ?>. These are known as start-tag and end tag.
<?php echo 'This is a PHP script'; ?>.
Here ‘echo’ is used to display the output of any variable which holds information as a container. We will
discuss the PHP syntax variables data types later on. All the PHP statements end with a semicolon(;).
How to place HTML elements inside the PHP tag
As I told you earlier, a PHP file may contain an HTML element. Here we will see some examples of how to put HTML elements are placed inside the PHP tag one by one.
<?php ?> <p>This is a simple text</p> $txt = "This is my first paragraph"; <p><?php echo $txt; ?></p> <?php ?>
<?php <p>This is a simple text</p> $txt = "This is my first paragraph"; echo '<p>'.$txt.'</p>'; ?>
In the example above, There are two paragraph elements shown, the first paragraph is used if you want to show the paragraph
element only and the second paragraph element is used to display the output inside the paragraph element in the PHP tag.
<?php ?> <html></html> <?php ?>
<?php ?> <table> <thead> <th>Column name of the table</th> </thead> <tbody> <tr> <td><?php echo 'The variable';?> or the value of the column</td> </tr> </tbody> </table> <?php ?>
Same consideration is used here
Basic overview of PHP variables
PHP variables are kind of containers where information or data are stored inside the container. PHP variables are declared with a $ sign followed by the name of the variable.
Rules for declaring variables in PHP
- A PHP variable should begin with a letter or underscore character.
- .A PHP variable can contain a mix of letters, numbers and underscore character.
How to assign values to PHP variables
We can assign any kind of value to a PHP variable. Values may contain string values or integer values. For eg—–
<html> <body> <?php $txt = "This is my first PHP sript"; $int = 10; //display the variables echo $txt; echo "The value of integer variable is ".$int ?> </body> </html>
In this example, the integer variable is displayed inside the string with a (.)dot sign. To display the value of a variable with another variable, the (.)dot sign is used between the two variables.
Basic overview of PHP data types
PHP data types are basically string, float, boolean, array, null, etc. We can check the variables using the var_dump() function. It returns the type of the variable.
Below are some of the examples———–
<html> <body> <?php $a = 10; $b = 10.5; $c = array('English','Bengali','Hindi'); echo var_dump($a); //it return an integer
echo var_dump($b); //it returns floating point echo var_dump($c); //it returns an array ?> </body> </html>
Overview of some important string functions in PHP
The string is a sequence of characters.
strlen():- This function returns the length of the string.
$username = “myname”;
echo strlen($username); // It returns 6.
str_word_count():- This function counts the number of words in a string.
echo str_word_count(“This is my first PHP script”); // It returns 6
strrev():- It reverses the string name.
echo strrev(‘Hello World’); // It displays (dlrow olleh)
strpos():- Search for a specific text within a string.
echo strpos(“Hello world”,’world’); //It displays 6, string count starts from ‘0’.
str_replace():- It replaces text with some other text in a string.
echo str_replace(“hello”,”user”,”Hello friend”); //It returns “user friend”
Also read, a Basic overview of PHP and how it works
Conclusion:- I hope this tutorial will help you to understand the PHP syntax Variables Data Types
2 thoughts on “Introduction to PHP syntax Variables Data types and strings”
Good way of explaining, and good post to take data concerning my presentation subject, which i am
going to deliver in school.
Wow, wonderful weblog layout! How lengthy have you ever been blogging
for? you make running a blog look easy. The overall look of your web site is excellent,
as neatly as the content material!