overview of PHP data value types

We are talking about the types for PHP data

  • values,
  • variables,
  • and read formats (literals).

Literals are data values that are explicitly spelled out in a PHP statement or expression.

In PHP the type of a variable is the type of its current value (It is not so in other programming languages.) Also, it is not necessary to declare the type for a variable (or define the value of a variable) before it is used. However, you should, at least, define a variable’s value before using it; otherwise, your script will error out with a warning. NULL is a good value to use as a default value. You will understand why later.

When PHP sees a variable (or literal) in a statement the type will be assumed to be what makes sense for the context. For example, if you use an integer variable in a string expression, PHP will interpret that integer variable as a string.

There are three things which matter about a variable:

  1. its type
  2. its value
  3. whether it is set or not set

Surprisingly, #3 can be just as important. For example, after a form is submitted as the result of a button press, the variable corresponding to that button will be set. You may not care about its value; however, you’ll care that it was set. Use isSet() to find out.

The PHP types are: integers, doubles, booleans, NULL, strings, arrays, objects and resources.

NULL

  • evaluates to FALSE as a boolean
  • returns FALSE when tested with isSet()
  • (when assigned to a variable which is passed as a function parameter) that variable is considered to be set

There are ways to coerce PHP into interpreting things to the type of your choice. See coercion.

Also see testing for the type.

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in Coding, PHP Data Value Types and tagged , . Bookmark the permalink.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s