Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 15th, 2003, 04:10 PM
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to declare variables

can someone tell me as to how to declare variables in php.
like in asp if i need to declare a variable comname i write
dim comname ......

Reply With Quote
  #2 (permalink)  
Old December 15th, 2003, 04:45 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Variables are declared when first used in PHP, so no need for the following...

var $somevar;

In PHP, this is only neccessary when building a class, the var keyword when used inside of a class creates special class-scope static variables.

$myvar = "some value";
Automatically created as a String.

$myvar = 2;
Automatically created as an integer.

$myvar = (string) "Some text";
Typecast as a string explicitly (typecasting is optional in PHP for 99% of variable usages).

$myvar = (int) 45;
Typecast an integer.

When you compare values as in...
if($myvar == "some value")

If myvar does not exist PHP will temporarily create a copy of it in memory with null value for comparision purposes, if error_reporting is set to E_ALL PHP will issue a Notice level error complaining that the variable did not exist. A better method is to use isset() or empty()

if (isset($myvar) && $myvar == "some_value)

http://www.php.net/isset
http://www.php.net/empty

This alleviates that Notice level error.

Always best to read the manual... at
http://www.php.net/manual/en/

Pay special attention to section II, which contains basic syntax, control structures, predefined variables... and general language concepts implemented in PHP.

hth,
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
The Following User Says Thank You to richard.york For This Useful Post:
logangibs (April 30th, 2013)
  #3 (permalink)  
Old May 6th, 2011, 02:34 PM
Registered User
 
Join Date: May 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Most beginners do the mistakes when initializing and utilizing variables.Note that syntax of PHP are similar to C language but there is some extra things to be care when declaration and utilizing variables in PHP.

1.In PHP all variables are prefixed with $ sign.


Code:
<?php
	$Number=0; //Declares variable and initialize it to 0
?>
The above example is the example of variable declaration in PHP. Read below tutorial for full details.

How to Declare Variables in PHP?
Reply With Quote
  #4 (permalink)  
Old September 29th, 2011, 10:57 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Re: How to declare variables in php

Hello Everyone,
Variable is nothing more than a name of data storage location in computer memory. Variables are used for storing values, like text strings, numbers or arrays............................................ ....
for more details please check out the following link.

http://mindstick.com/Articles/d27eca...HP%20Variables

Thanks !!!!!
Reply With Quote
  #5 (permalink)  
Old October 7th, 2011, 02:46 AM
Banned
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Variables are used for storing values, like text strings, numbers or arrays.
When a variable is declared, it can be used over and over again in your script.
All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
$var_name = value;
_____________
SAP Ireland
Reply With Quote
  #6 (permalink)  
Old October 15th, 2011, 09:30 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2011
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default variable

In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents. A variable name in computer source code is usually associated with a data storage location and thus also its contents, and these may change during the course of program execution.
Reply With Quote
  #7 (permalink)  
Old October 20th, 2011, 09:16 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One case when a variable should be declared in PHP is when coding dynamic web sites where a variable is given a value only under certain conditions. If the variable is NOT defined and the condition is NOT met, PHP will return the error 'Undefined variable...' when it will parse the variable.

'Define' is used with constants. A way to define a variable could be:

$variable_name = null;
Reply With Quote
  #8 (permalink)  
Old April 14th, 2012, 11:07 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Location: Hyderabad
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Hi,
Simple! A variable is declared in PHP as:
$varName = 10; # a variable holding an integer
$objName = "HalloWorld!"; # a variable holding a string

For more information, checkout PHP Manual at:
http://www.php.net/manual/en/language.variables.php

Thanks
bye
Badar
Reply With Quote
  #9 (permalink)  
Old June 19th, 2012, 12:33 PM
Authorized User
Points: 64, Level: 1
Points: 64, Level: 1 Points: 64, Level: 1 Points: 64, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?php
$a= b;

$ sign denotes its a variable
?>
Reply With Quote
  #10 (permalink)  
Old July 26th, 2012, 07:16 AM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2012
Location: Pune
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Quote:
Originally Posted by richard.york View Post
Variables are declared when first used in PHP, so no need for the following...

var $somevar;

In PHP, this is only neccessary when building a class, the var keyword when used inside of a class creates special class-scope static variables.

$myvar = "some value";
Automatically created as a String.

$myvar = 2;
Automatically created as an integer.

$myvar = (string) "Some text";
Typecast as a string explicitly (typecasting is optional in PHP for 99% of variable usages).

$myvar = (int) 45;
Typecast an integer.

When you compare values as in...
if($myvar == "some value")

If myvar does not exist PHP will temporarily create a copy of it in memory with null value for comparision purposes, if error_reporting is set to E_ALL PHP will issue a Notice level error complaining that the variable did not exist. A better method is to use isset() or empty()

if (isset($myvar) && $myvar == "some_value)

http://www.php.net/isset
http://www.php.net/empty

This alleviates that Notice level error.

Always best to read the manual... at
http://www.php.net/manual/en/

Pay special attention to section II, which contains basic syntax, control structures, predefined variables... and general language concepts implemented in PHP.

hth,
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Hi richard.york;

Thank you for your detailed suggestion about declaring a variable in PHP.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Namespace - how to declare? JillB Excel VBA 0 January 25th, 2008 06:11 PM
declare My ajkumar Visual Basic 2005 Basics 0 April 18th, 2007 05:24 AM
HOW DECLARE VARIABLES IN PHP jhan316 Beginning PHP 1 July 25th, 2006 07:16 PM
Can I declare variables visable from every script Makis Beginning PHP 1 January 11th, 2004 01:02 AM
how to declare date variables rehana Beginning PHP 1 December 15th, 2003 04:56 PM



All times are GMT -4. The time now is 05:16 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.