Header Ads

PHP Data Types, Strings, Constant and Operator.

PHP Data Types

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:
  • String
  • Integer
  • Float (floating point numbers - also called double)
  • 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:

<?php
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>";
echo $y;
?>

PHP Integer

An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.

<?php
$x = 5985;
var_dump($x);
?>

PHP Float

A float (floating point number) is a number with a decimal point or a number in exponential form.

In the following example $x is a float. The PHP var_dump() function returns the data type and value:

<?php
$x = 10.365;
var_dump($x);
?>

PHP 5 Strings



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





PHP String Functions




Get The Length of a String




The PHP strlen() function returns the length of a string.


<?php
echo strlen("Hello world!"); // outputs 12
?>
The output of the code above will be: 12.

Count The Number of Words in a String


The PHP str_word_count() function counts the number of words in a string:

<?php
echo str_word_count("Hello world!"); // outputs 2
?>

The output of the code above will be: 2.

Reverse a String

The PHP strrev() function reverses a string:

<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>

The output of the code above will be: !dlrow olleH.


PHP 5 Constants


Constants are like variables except that once they are defined they cannot be changed or undefined.




PHP Constants


A constant is an identifier (name) for a simple value. The value cannot be changed during the script.



A valid constant name starts with a letter or underscore (no $ sign before the constant name).



Create a PHP Constant




To create a constant, use the define() function.

Syntax

define(namevaluecase-insensitive)

PHP 5 Operators



Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators

1 comment:

Powered by Blogger.