Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2003, 05:10 PM
Registered User
 
Join Date: Dec 2003
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 ......

 
Old December 15th, 2003, 05:45 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 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
:::::::::::::::::::::::::::::::::
The Following User Says Thank You to richard.york For This Useful Post:
logangibs (April 30th, 2013)
 
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?
 
Old September 29th, 2011, 10:57 AM
Registered User
 
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 !!!!!
 
Old October 7th, 2011, 02:46 AM
alexa007
Guest
 
Posts: n/a
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
 
Old October 15th, 2011, 09:30 PM
Registered User
 
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.
 
Old October 20th, 2011, 09:16 AM
Registered User
 
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;
 
Old April 14th, 2012, 11:07 AM
Registered User
 
Join Date: Apr 2012
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
 
Old June 19th, 2012, 12:33 PM
Authorized User
 
Join Date: May 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?php
$a= b;

$ sign denotes its a variable
?>
 
Old July 26th, 2012, 07:16 AM
Registered User
 
Join Date: Jul 2012
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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Namespace - how to declare? JillB Excel VBA 0 January 25th, 2008 07: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 02:02 AM
how to declare date variables rehana Beginning PHP 1 December 15th, 2003 05:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.