Type casting in php javatpoint
PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types: Show
PHP Data Types: Scalar TypesIt holds only single value. There are 4 scalar data types in PHP.
PHP Data Types: Compound TypesIt can hold multiple values. There are 2 compound data types in PHP.
PHP Data Types: Special TypesThere are 2 special data types in PHP.
PHP BooleanBooleans are the simplest data type works like switch. It holds only two values: TRUE (1) or FALSE (0). It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE. Example: Output: PHP IntegerInteger means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points. Rules for integer:
Example: Output: Decimal number: 34 Octal number: 163 HexaDecimal number: 69 PHP FloatA floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign. Example: Output: Addition of floating numbers: 73.812 PHP StringA string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters. String values must be enclosed either within single quotes or in double quotes. But both are treated differently. To clarify this, see the example below: Example: Output: Hello Javatpoint Hello $company PHP ArrayAn array is a compound data type. It can store multiple values of same data type in a single variable. Example: Output: array(3) { [0]=> string(13) "Royal Enfield" [1]=> string(6) "Yamaha" [2]=> string(3) "KTM" } Array Element1: Royal Enfield Array Element2: Yamaha Array Element3: KTM You will learn more about array in later chapters of this tutorial. PHP objectObjects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared. Example: Output: Bike Model: Royal Enfield This is an advanced topic of PHP, which we will discuss later in detail. PHP ResourceResources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. It is an external resource. This is an advanced topic of PHP, so we will discuss it later in detail with examples. PHP NullNull is a special data type that has only one value: NULL. There is a convention of writing it in capital letters as it is case sensitive. The special type of data type NULL defined a variable with no value. Example: Output: Summary: in this tutorial, you’ll learn about the PHP type casting that allows you to convert a value of a type to another. Introduction to the PHP type castingType casting allows you to convert a value of one type to another. To cast a value, you use the following type casting operators:
Let’s take some examples of using the type casting operators. Cast to an integerTo cast a value to an integer, you use the The
Suppose you have a string and want to cast it as an integer:
The result may not be what you expected. If a string is numeric or leading numeric, then the
In this example, the Note that the
Cast to a floatTo cast a value to a float, you use the
Cast to a stringTo cast a value to a string, you use the The following example uses the
You don’t need to use the
The
Output:
The The
And you’ll get a warning that you’re attempting to convert an array to a string.
Summary
Did you find this tutorial useful? What is the type casting in PHP?PHP type casting allows you to convert a value from one type to another. Use a type casting operator to cast a value to the desired type.
What is type casting explain with example?Typecasting, or type conversion, is a method of changing an entity from one data type to another. It is used in computer programming to ensure variables are correctly processed by a function. An example of typecasting is converting an integer to a string.
What is type casting in PHP w3schools?Type casting is when you assign a value of one data type to another type.
What is type casting in OOP?Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size. byte -> short -> char -> int -> long -> float -> double.
|