How to Use Data Type String in PHP

How to use data type string in PHP? It’s the basic question in PHP, mostly newbies have this question in their mind. PHP has some data types like String, Integer, Float, Boolean, Array, Object, NULL, and Resource. The string is one of the most important data type in PHP. Today I am going to teach you, how to use data type string in PHP. Let’s move.

How to Use Data Type String in PHP:

Data type string i. e. you can say, whatever you will type, it will be displayed. You can’t make calculations in this data type. You can type even numbers too in this data type, but these numbers would also be in string format. It means you are not able to make calculations in these numbers.

Data Type String

Data Type String As Text:

If you want to write text in PHP script use the following pattern, first I have declared a variable $name with value of John, then I have printed it with an echo statement. You can do that too.

<?php
$name = "John";
echo $name:
?>

If you rung the above PHP script in any PHP page, the output will be John.

Data Type String As Numeric:

If you want to write numeric numbers in PHP script use the following pattern, first I have declared a variable $age with value of 33, then I have printed it with echo statement. You can do that too.

<?php
$age = "33;
echo $age;
?>

If you rung the above PHP script in any PHP page, the output will be 33.

Note:

Remember that we usually consider numeric numbers as data type integer. but in the above example, we used $age variable as data type string. If you don’t use double quotation around 33, then it will convert in data type integer. But in example, above the value of variable $age is treated as data type string.

Data Type String As Date of Birth:

If you want to write date of birth in PHP script use the following pattern, first I have declared a variable $date_of_birth with value of 13-07-1960, then I have printed it with an echo statement. You can do that too.

<?php
$date_of_birth = "13-07-1960";
echo $date_of_birth;
?>

If you run the above PHP script in any PHP page, the output will be 13-07-1960.

Note:

Remember that usually we consider date of birth as data type float for making a calculation of days in age. But in above example I use date of birth as data type string. It means in this example date of birth perform as display purposes not for making calculations of days of age. If you want to make calculations in date of birth you need to convert this date of birth into data type float. We will learn it letter, because it’s beyond of this scope of this topic.

How to Connect String Variables:

Some time you need to connect string variables each other. This process is called concatenating strings. How to connect string variables each other, if you don’t know how to do this? Just follow these steps.

<?php
$name "John";
$profession = "Software Enginee";
$residence = "Finland";

$bio_data = $name . $profession . $residence;
echo $bio_data;
?>

In above example first I have declared three variable $name, $profession and $residence with different values then these variables are connected to each other. And in last I have printed these variables with echo statement.

You can notice that there is no space among John, Finland and Software Engineer. For applying space among these variables you can reassign values of $bio_data variable as follows:

$bio_data = $name . " " . $profession . " " . $residence;

Now you can check $bio_data variable’s value by printing with echo statement again. as follows:

echo $bio_data;

If you don’t know how to declare variables in PHP, don’t worry about it, I have written a through tutorial about this topic. You can read this post too How to Declare Variable in PHP

Dear friends if you want to create polling system in PHP, and you don’t know how to do this, just go to this article, I hope it will help you. 14 Best PHP Scripts for Polling System

Leave a Reply

Your email address will not be published. Required fields are marked *