/    Sign up×
Community /Pin to ProfileBookmark

I am writing a questionaire in PHP whose responses will be “numbers” from 1 to 8. The HTML form will pass these responses to the PHP program as text, the pair values being the numbers of the items and corresponding user responses (1 to 8). How can these text values be converted to numeric values so that arithmetic can be performed? The book I am using to investigate PHP does not cover this topic. I am familiar with how this is accomplished in other programming languages.Thank you.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@pyroMar 21.2004 — PHP does not require you to set the type definition in your variables declaration. The type is determined by the variables use. Take this example:

[code=php]$foo = 5; # $foo is cast as an integer
$bar = "5"; # $bar is cast as an string
$bar = $foo + $bar; # $bar is now an integer, due to the addition[/code]


See http://us4.php.net/language.types.type-juggling for more information on this.
Copy linkTweet thisAlerts:
@PittsburghRedauthorMar 22.2004 — Thank you Pyro. I understand. I did not provide a variable to cast. I wrote:

print "<H3>The sum is $n01+$n02+$n03</H3>";

which printed the exact line between quotes. When I switched to:

$sum=$n01+$n02+$n03;

print "<H3>The sum is $sum.</H3>";

The actual numeric sum of the values was printed out.
Copy linkTweet thisAlerts:
@pyroMar 22.2004 — The first example didn't work because you were using PHP's variable interpolation, which will not add the variables but rather output something like this (assuming your variables values were 1, 2, and 3, repectively):

The sum is 1+2+3

This, however, would output the desired result:

[code=php]print "<H3>The sum is ".$n01+$n02+$n03."</H3>";[/code]
Copy linkTweet thisAlerts:
@PittsburghRedauthorMar 22.2004 — The first example didn't work because you were using PHP's variable interpolation, which will not add the variables but rather output something like this (assuming your variables values were 1, 2, and 3, repectively):

The sum is 1+2+3


No, it printed the identifiers (var names) ... not the indicated sum of the values stored in the vars.

The syntax you indicate below is most interesting. I did not see this in my book. I will try it.


This, however, would output the desired result:



PHP:
--------------------------------------------------------------------------------



print "<H3>The sum is ".$n01+$n02+$n03."</H3>";


--------------------------------------------------------------------------------




__________________
×

Success!

Help @PittsburghRed spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...