Strings in PHP: Manipulation and Examples

Rumman Ansari   Software Engineer   2024-07-09 10:04:07   5718  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

PHP data types are used to hold different types of data or values. PHP supports the following data types:

  • String
  • Integer
  • Float
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

PHP String

A string is a sequence of characters, like "Hello world!".

A string can be any text inside quotes. You can use single or double quotes:

Example code:

<span class="pln">
</span><span class="pun">&lt;?</span><span class="pln">php

$x </span><span class="pun">=</span><span class="pln"> </span><span class="str">"Hello world!"</span><span class="pun">;</span><span class="pln">
$y </span><span class="pun">=</span><span class="pln"> </span><span class="str">'Hello world!'</span><span class="pun">;</span><span class="pln">

echo $x</span><span class="pun">;</span><span class="pln">
echo </span><span class="str">"&lt;br&gt;"</span><span class="pun">;</span><span class="pln">
echo $y</span><span class="pun">;</span><span class="pln">

</span><span class="pun">?&gt;</span><span class="pln">

</span>

Output:

The above code will produce the following result-

<span class="pln">
</span><span class="typ">Hello</span><span class="pln"> world</span><span class="pun">!</span><span class="pln">
</span><span class="typ">Hello</span><span class="pln"> world</span><span class="pun">!</span><span class="pln">
</span>

PHP Resource

The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.

A common example of using the resource data type is a database call.

We will talk about the resource type in advanced topic.

MCQ Available

There are 14 MCQs available for this topic.

14 MCQTake Quiz

No Questions Data Available.
No Program Data.

Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.