PHP Variables: PHP Variable: Declaring String, Integer, and Float

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

PHP Variables

<="" p="">

In PHP, a variable is declared using a $ sign followed by the variable name. Here, some imp
variables:

o As PHP is a loosely typed language, so we do not need to declare the data types of t
analyzes the values and makes conversions to its correct datatype.
o After declaring a variable, it can be reused throughout the code.
o Assignment Operator (=) is used to assign the value to a variable.

Syntax of declaring a variable in PHP is given below:

1. $variablename=value;

Rules for declaring PHP variable:

o A variable must start with a dollar ($) sign, followed by the variable name.
o It can only contain alpha-numeric character and underscore (A-z, 0-9, _).
o A variable name must start with a letter or underscore (_) character.
o A PHP variable name cannot contain spaces.
o One thing to be kept in mind that the variable name cannot start with a number or special
o PHP variables are case-sensitive, so $name and $NAME both are treated as different variab

PHP Variable: Declaring string, integer, and float


Let's see the example to store string, integer, and float values in PHP variables.

File: variable1.php

1. <?php
2. $str="hello string";
3. $x=200;
4. $y=44.6;
5. echo "string is: $str <br/>";
6. echo "integer is: $x <br/>";
7. echo "float is: $y <br/>";
8. ?>

Output:

string is: hello string


integer is: 200
float is: 44.6

PHP Variable: Sum of two variables


File: variable2.php

1. <?php
2. $x=5;
3. $y=6;
4. $z=$x+$y;
5. echo $z;
6. ?>

Output:

11

PHP Variable: case sensitive


In PHP, variable names are case sensitive. So variable name "color" is different from Color, COLOR

File: variable3.php

1. <?php
2. $color="red";
3. echo "My car is " . $color . "<br>";
4. echo "My house is " . $COLOR . "<br>";
5. echo "My boat is " . $coLOR . "<br>";
6. ?>

Output:

My car is red
Notice: Undefined variable: COLOR in C:\wamp\www\variable.php on line 4
My house is
Notice: Undefined variable: coLOR in C:\wamp\www\variable.php on line 5
My boat is
PHP Variable: Rules
PHP variables must start with letter or underscore only.

PHP variable can't be start with numbers and special symbols.

File: variablevalid.php

1. <?php
2. $a="hello";//letter (valid)
3. $_b="hello";//underscore (valid)
4.
5. echo "$a <br/> $_b";
6. ?>

Output:

hello
hello

File: variableinvalid.php

1. <?php
2. $4c="hello";//number (invalid)
3. $*d="hello";//special symbol (invalid)
4.
5. echo "$4c <br/> $*d";
6. ?>

Output:

Parse error: syntax error, unexpected '4' (T_LNUMBER), expecting variable (T_VARIABLE
or '$' in C:\wamp\www\variableinvalid.php on line 2

PHP: Loosely typed language


PHP is a loosely typed language, it means PHP automatically converts the variable to its correct d

← PrevNext →
ADVERTISEMENT
ADVERTISEMENT

For Videos Join Our Youtube Channel: Join


Now

Feedback

o Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

Learn Latest Tutorials

Splunk

SPSS

Swagger

Transact-SQL
Tumblr

ReactJS

Regex

Reinforcement Learning

R Programming

RxJS

React Native

Python Design Patterns

Python Pillow
Python Turtle

Keras

Preparation

Aptitude

Reasoning

Verbal Ability

Interview Questions

Company Questions

Trending Technologies
Artificial Intelligence

AWS

Selenium

Cloud Computing

Hadoop

ReactJS

Data Science

Angular 7

Blockchain
Git

Machine Learning

DevOps

ADVERTISEMENT

B.Tech / MCA

DBMS

Data Structures

DAA

Operating System

Computer Network
Compiler Design

Computer Organization

Discrete Mathematics

Ethical Hacking

Computer Graphics

Software Engineering

Web Technology

Cyber Security

Automata
C Programming

C++

Java

.Net

Python

Programs

Control System

Data Mining

Data Warehouse
ADVERTISEMENT

Type System ¶
PHP uses a nominal type system with a strong behavioral subtyping relation. The
subtyping relation is checked at compile time whereas the verification of types is
dynamically checked at run time.

PHP's type system supports various atomic types that can be composed together to
create more complex types. Some of these types can be written as type declarations.

Atomic types ¶

Some atomic types are built-in types which are tightly integrated with the language and
cannot be reproduced with user defined types.

The list of base types is:

o Built-in types

o null type

o Scalar types:

o bool type

o int type

o float type

o string type

o array type

o object type

o resource type

o never type

o void type

o Relative class types: self, parent, and static

o Value types
o false

o true

o User-defined types (generally referred to as class-types)

o Interfaces

o Classes

o Enumerations

o callable type

Composite types ¶

It is possible to combine multiple atomic types into composite types. PHP allows types
to be combined in the following ways:

o Intersection of class-types (interfaces and class names).

o Union of types.

Intersection types ¶

An intersection type accepts values which satisfies multiple class-type declarations,


rather than a single one. Individual types which form the intersection type are joined by
the & symbol. Therefore, an intersection type comprised of the types T, U, and V will be
written as T&U&V.

Union types ¶

A union type accepts values of multiple different types, rather than a single one.
Individual types which form the union type are joined by the | symbol. Therefore, a
union type comprised of the types T, U, and V will be written as T|U|V. If one of the
types is an intersection type, it needs to be bracketed with parenthesis for it to written
in DNF: T|(X&Y).

Type aliases ¶

PHP supports two type aliases: mixed and iterable which corresponds to the union
type of object|resource|array|string|float|int|bool|null and Traversable|
array respectively.

Nota: PHP does not support user-defined type aliases.


+add a note

User Contributed Notes


There are no user contributed notes for this page.
 Tipos
 Introducción
 Type System
 NULO
 Booleanos
 Números enteros (Integers)
 Números de punto flotante
 Cadenas de caracteres (Strings)
 Numeric strings
 Arrays
 Objetos
 Enumerations
 Recursos
 Llamadas de retorno (Callbacks / Callables)
 Mixed
 Void
 Never
 Relative class types
 Value types
 Iterables
 Declaraciones de tipo
 Manipulación de tipos

 Copyright © 2001-2024 The PHP Group

 My PHP.net

 Contact

 Other PHP.net sites

 Privacy policy

PHP (Hypertext Preprocessor) is a widely-used server-side scripting language


designed for web development. It is especially suited for creating dynamic web
pages and applications. PHP scripts are executed on the server, and the result is
sent to the client's web browser, which then renders the page.

Here are some key features and aspects of PHP:


1. Syntax: PHP syntax is similar to C and Perl, making it easy to learn for
those familiar with these languages. It is embedded within HTML code.
phpCopy code
<?php // PHP code goes here ?>
2. Server-Side Scripting: PHP is a server-side scripting language, meaning
that it runs on the web server, not on the client's browser. The server
processes the PHP code and sends the output (usually HTML) to the
client's browser.
3. Open Source: PHP is an open-source scripting language, which means
its source code is freely available and can be modified and redistributed.
4. Platform Independence: PHP is platform-independent, meaning it can
run on various operating systems, including Windows, Linux, macOS, and
others.
5. Database Integration: PHP has strong support for database integration,
and it is often used to interact with databases such as MySQL,
PostgreSQL, SQLite, and others.
6. Frameworks: There are several PHP frameworks, such as Laravel,
Symfony, CodeIgniter, and Zend, that provide a structured way to build
web applications, promoting code organization and reusability.
7. Community Support: PHP has a large and active community of
developers. There are numerous online resources, forums, and
documentation available for PHP development.
8. Security: PHP has built-in security features, and developers can follow
best practices to ensure the security of their applications. However, as
with any programming language, it's important for developers to be
aware of common security pitfalls.

Here's a simple example of a PHP script that outputs "Hello, World!":

phpCopy code
<?php echo "Hello, World!" ; ?>

This script uses the echo statement to output the string "Hello, World!" to the
browser.

Remember that PHP is often used in conjunction with HTML to create dynamic
and interactive web pages. It is a versatile language with a long history in web
development.

You might also like