PHP
  Home arrow PHP arrow Page 2 - Working With PHP Data Types
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Working With PHP Data Types
By: Steve Adcock
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 12
    2002-01-23

    Table of Contents:
  • Working With PHP Data Types
  • Defining variables and constants
  • Manipulating a variables data type
  • If this, if that
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More PHP Articles
    More By Steve Adcock


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT