CodeBlanks Logo Icon CodeBlanks

string and int

PHP course · Beginner · Lesson 13/116

Identify and use the string and int data types.

What are string and int?

In PHP, data is stored in different types. The first two scalar types are string and integer.

A string holds text; it is written in single or double quotes. Example: $name = "Alex";

An integer (or int) holds a whole number; a number with no decimal point. Example: $age = 25;

You can combine the two types with echo to print them. Knowing the types prevents surprising results.

Two ways to define a string

A string can be written two ways: double quotes "text" or single quotes 'text'.

In double quotes, variables are interpolated: echo "Hello $name"; prints Hello Alex.

In single quotes, variables are not interpolated: echo 'Hello $name'; prints Hello $name.

Use single quotes for fixed text; use double quotes when you need to embed variables.

Exercise Print the product name and price

Assign "Coffee" to $productName and 45 to $price. Then use echo to print them in the form Coffee: 45 TL; end the line with \n.

Show example solution
" . $price . " TL\n".
Example solution:
<?php
$productName = "Coffee";
$price = 45;
echo $productName . ": " . $price . " TL\n";

Test yourself

  1. Which one is a string value?

    $name = "Alex";
    • $count = 25;
    • $name = "Alex";
    • $price = 12.5;
    • $active = true;
    Show answer

    $name = "Alex"; — Correct! A value in quotes is a string.

  2. Assign the city name as a string

    $city = ____;
    Show answer
    $city = "Istanbul";
  3. Which one is a valid int declaration?

    • $count = 42;
    • $count = "42";
    • $count = 42.0;
    • $count = true;
    Show answer

    $count = 42; — Correct! 42 is a whole number with no decimal point, so it is an int.

All lessons in Beginner