0% found this document useful (0 votes)
41 views

Phpnotes

Variables in PHP are containers that hold values and are preceded by a dollar sign. Variable names can include letters, numbers, and underscores but cannot include spaces and must begin with a letter or underscore. PHP supports standard data types like integers, strings, doubles, and booleans that can be assigned to variables and their types checked with functions like gettype() and changed with settype() or type casting.

Uploaded by

muralidhransiva
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Phpnotes

Variables in PHP are containers that hold values and are preceded by a dollar sign. Variable names can include letters, numbers, and underscores but cannot include spaces and must begin with a letter or underscore. PHP supports standard data types like integers, strings, doubles, and booleans that can be assigned to variables and their types checked with functions like gettype() and changed with settype() or type casting.

Uploaded by

muralidhransiva
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

php

Variables

A variable is a special container that you can define to "hold" a value. Variables are fundamental
to programming. preceded by a dollar sign ($). Variable names can include letters, numbers, and the
underscore character (_). They cannot include spaces. They must begin with a letter or an underscore.

print (2 + 4);

$a;
$a_longish_variable_name;
$2453;
$sleepyZZZZ;

you usually assign a value to it in the same statement, as shown here:

$num1 = 8; print $num1;


$num2 = 23;

Standard Data Types


<html>
<head>
<title>Listing 4.1 Testing the type of a
variable</title>
</head>
5: <body>
6: <?php
7: $testing; // declare without assigning NULL
8: print gettype( $testing ); // null integer
9: print "<br>"; string
10: $testing = 5; double
11: print gettype( $testing ); // integer boolean
12: print "<br>";
13: $testing = "five";
14: print gettype( $testing ); // string
15: print("<br>");
16: $testing = 5.0;
17: print gettype( $testing ); // double
18: print("<br>");
19: $testing = true;
20: print gettype( $testing ); // boolean
21: print "<br>";
22: ?>
23: </body>
24: </html>

Changing the Type of a Variable with settype()

<?php
7: $undecided = 3.14;
8: print gettype( $undecided ); // double
9: print " is $undecided<br>"; // 3.14
10: settype( $undecided, 'string' );
22: ?>
23: </body>
24: </html>

Changing Type by Casting

By placing the name of a data type in parentheses in front of a variable, you create a copy of that
variable's value converted to the data type specified.

<?php
7: $undecided = 3.14;
8: $holder = ( double ) $undecided;
9: print gettype( $holder ) ; // double
10: print " is $holder<br>"; // 3.14
11: $holder = ( string ) $undecided;
12: print gettype( $holder ); // string
13: print " is $holder<br>"; // 3.14
: $holder = ( integer ) $undecided;
20: $holder = ( boolean ) $undecided;

The Assignment Operator

You have seen the assignment operator each time we have initialized a variable. It consists of the
single character =.

print ($name = "matt");

The Concatenation Operator

The concatenation operator is represented by a single period.

it appends the right-hand operand to the left. So

"hello"." world" Returns "hello world"

Combined Assignment Operators

$x = 4;
$x = $x + 4; // $x now equals 8

$x = 4;
$x += 4; // $x now equals 8

You might also like