Home arrow PHP arrow Page 2 - Working With PHP Data Types
PHP

Working With PHP Data Types


Using and manipulating variables in PHP is extremely easy because PHP includes several functions to help us compare and contrast them. In this article, Steve introduces us to the range of PHP data types. He also teaches us how to work with and manipulate PHP data types. This article also presents several code examples that you can test and learn from.

Author Info:
By: Steve Adcock
Rating: 4 stars4 stars4 stars4 stars4 stars / 16
January 23, 2002
TABLE OF CONTENTS:
  1. · Working With PHP Data Types
  2. · Defining variables and constants
  3. · Manipulating a variables data type
  4. · If this, if that
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Working With PHP Data Types - Defining variables and constants
(Page 2 of 5 )

Finding your PHP info

You can easily find out some detailed parameter values and options regarding your PHP installation using PHP's phpinfo() function. PHP's code delimiters are <? and ?> and must surround all of your PHP code. The following code will display in-depth information about your PHP installation:

<?

phpinfo();

?>


A cool little pre-defined PHP constant will let you view the type of operating system that your PHP installation is running on. To view it in your browser, use the following code:

<?

echo(PHP_OS);

?>


The echo syntax simply displays whatever value is within the parenthesis; in this case, the value of the constant variable, PHP_OS will be displayed.

Defining variables and constants

A programming language is useless without variables and constants. Variables and constants are the building blocks of any programming language and contain memory addresses that represent where a specific value is stored. A variable's value can change, while a constant's value is, as you might have guessed, fixed.

Let's look at how you would define a variable. Note that two slashes (//) define a comment in PHP, and will not be displayed in your browser.

<?

$i = 10; // Defines i as integer 10

$k = 12.1; // Defines k as a double, 12.1 (integer type)

$k = (int) $k; // Redefines the value of k (12.1) to an integer, or 12

$z = $i + 20; // Defines z as the value of i + 20, or 30

$team = "Broncos"; // Defines team as Broncos

?>


Notice a few things about the PHP code shown above. First, each variable is defined with a dollar sign ($) prepended to the variable's name. In addition, a semicolon is used to separate each variables declaration. Lastly, strings are defined within quotation marks, while integers are not.

Let's create some constant variables. Constants are created using the define function. The signature of the define function looks like this:

define(string constantName, mixed value);

<?

define("COLOR", "green"); // Defines COLOR as green

define("JOB", "supervisor"); // Defines JOB as supervisor

define("SALARY", 100000); // Defines SALARY as 100000

?>


Notice again that strings are written with quotation marks around the value, while integers are not. Defining variables and constants is one thing, but how can we actually display the values of the constant variables that what we have defined?

<?

// First, we will display our variables

$i = 10;

$k = 12.1;

$k = (int) $k;

$z = $i + 20;

$team = "Broncos";

echo("$i <br>"); // Displays 10

echo("$k <br>"); // Displays 12

echo("$z <br>"); // Displays 30

echo("$team <br> <br>"); // Displays Broncos

// Now, let's display the constants

define("COLOR", "green"); // Defines COLOR as green

define("JOB", "supervisor"); // Defines JOB as supervisor

define("SALARY", 100000); // Defines SALARY as 100000

echo(COLOR . "<br>"); // Displays green

echo(JOB . "<br>"); // Displays supervisor

echo(SALARY . "<br>"); // Displays 100000

?>


To make this code example easier to understand, I have reused the variables and constants that we had defined a little earlier. Displaying constants is slightly different to displaying standard variables. Most notably, when we refer to a constant value, we don't prepend it with a dollar sign.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials