PHP Tutorial: Output, Data Types, Strings, and Numbers

Table of Contents

  1. Outputting Data
  2. PHP Data Types
  3. Working with Strings
  4. Working with Numbers

Outputting Data

PHP provides two basic ways to output data: echo and print. Both are language constructs and can be used without parentheses. However, they do have slight differences:

Echo

  • echo can output one or more strings.
  • It is marginally faster than print.
  • Does not return any value.

Syntax:

echo "Hello World!";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";

Print

  • print can output only one string.
  • Returns 1, so it can be used in expressions.

Syntax:

print "Hello World!";

Example:

<?php
echo "Hello World!";
print "Hello World!";
?>

PHP Data Types

PHP supports various data types, which can be categorized as follows:

  1. String
  2. Integer
  3. Float (floating point numbers – also called double)
  4. Boolean
  5. Array
  6. Object
  7. NULL
  8. Resource

Example:

<?php
$string = "Hello World!";
$integer = 123;
$float = 12.34;
$boolean = true;
$array = array("Apple", "Banana", "Cherry");
$object = (object) ['property' => 'value'];
$null = NULL;

echo $string;
?>

Working with Strings

Strings in PHP are sequences of characters. PHP provides several functions and operators for string manipulation.

Creating Strings

Strings can be created using single quotes, double quotes, or the heredoc and nowdoc syntax.

Single Quotes:

echo 'Hello World!';

Double Quotes:

echo "Hello World!";

Heredoc:

echo <<<EOT
Hello World!
EOT;

Nowdoc:

echo <<<'EOT'
Hello World!
EOT;

String Functions

PHP provides a rich set of functions for string manipulation:

  • strlen(): Returns the length of a string.
  • str_word_count(): Counts the number of words in a string.
  • strrev(): Reverses a string.
  • strpos(): Searches for a specific text within a string.
  • str_replace(): Replaces text within a string.

Example:

<?php
$string = "Hello World!";

echo strlen($string); // Outputs: 12
echo str_word_count($string); // Outputs: 2
echo strrev($string); // Outputs: !dlroW olleH
echo strpos($string, "World"); // Outputs: 6
echo str_replace("World", "Dolly", $string); // Outputs: Hello Dolly!
?>

Working with Numbers

PHP supports integers and floating-point numbers and provides a variety of functions for number manipulation.

Integers

  • Integers are non-decimal numbers between -2,147,483,648 and 2,147,483,647.
  • They can be specified in decimal (base 10), hexadecimal (base 16 – prefixed with 0x), octal (base 8 – prefixed with 0), and binary (base 2 – prefixed with 0b).

Example:

<?php
$decimal = 123;
$hex = 0x1A;
$octal = 0123;
$binary = 0b11111111;

echo $decimal; // Outputs: 123
echo $hex; // Outputs: 26
echo $octal; // Outputs: 83
echo $binary; // Outputs: 255
?>

Floats

  • Floats (floating point numbers) are decimal numbers.
  • They can be specified in normal decimal notation or scientific notation.

Example:

<?php
$float = 1.234;
$scientific = 0.1234E4;

echo $float; // Outputs: 1.234
echo $scientific; // Outputs: 1234
?>

Number Functions

PHP provides several functions for number manipulation:

  • is_int(): Checks if the variable is an integer.
  • is_float(): Checks if the variable is a float.
  • is_numeric(): Checks if the variable is a number or a numeric string.
  • intval(): Returns the integer value of a variable.
  • floatval(): Returns the float value of a variable.

Example:

<?php
$number = 123;
$stringNumber = "123.45";

echo is_int($number); // Outputs: 1 (true)
echo is_float($number); // Outputs: (false)
echo is_numeric($stringNumber); // Outputs: 1 (true)
echo intval($stringNumber); // Outputs: 123
echo floatval($stringNumber); // Outputs: 123.45
?>

Conclusion

This tutorial provided an overview of PHP’s basic functionalities, including outputting data with echo and print, understanding different data types, and working with strings and numbers. Mastering these basics is essential for any PHP developer, providing a foundation for more advanced topics.

Leave a Comment

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

Scroll to Top