PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

unserialize> <settype
Last updated: Fri, 01 Jun 2007

view this page in

strval

(PHP 4, PHP 5)

strval — Get string value of a variable

Description

string strval ( mixed $var )

Returns the string value of var. See the documentation on string for more information on converting to string.

var may be any scalar type. You cannot use strval() on arrays or objects.

See Also

floatval()
intval()
settype()
Type juggling



add a note add a note User Contributed Notes
strval
worldsaver at phpfreaks dot sw
05-Jun-2007 08:26
<? session_start(); ?>

<table>
<form method="post" name="frm1">
    <tr><td> Gebruikersnaam: </td> <td> <input type="text" name="txtGebruiker" /> </td></tr>
    <tr><td> Paswoord: </td> <td> <input type="text" name="txtPaswoord" /> </td></tr>
    <tr><td></td><td> <input type="submit" name="btnInloggen" value="Inloggen" /> </td></tr>
</form>
</table>

<?
if($_POST['btnInloggen']=="Inloggen") {
    include(
"includes/connect.php");
   
$Gebruiker = $_POST['txtGebruiker'];
   
$Paswoord = $_POST['txtPaswoord'];
   
   
$query = "SELECT * FROM `tblgebruikers` WHERE gebruikersnaam = '$Gebruiker' AND wachtwoord =     '$Paswoord'";
   
$result = mysql_query($query);
   
mysql_close();
   
    if(
mysql_num_rows($result)==1) {
       
$_SESSION['Toegang'] = "Ja";
       
$_SESSION['Functie'] = mysql_result($result, 0, 'functie');
       
header("location: Hoofdpagina3.php");
    }else {
        echo
"Foutief ingelogd";
    }
}
?>

<? session_start();
if (
$_SESSION['Toegang']=="Ja") { ?>

<table border="1" width="100%">
<tr height="500" valign="top">
<td height="200">

<?
if($_SESSION['Functie']=="admin") { ?>
    <p><a href="Hoofdpagina3.php?item=ledenoverzicht"> Ledenoverzicht </a>
    <p><a href="Hoofdpagina3.php?item=ledentoevoegen"> Leden toevoegen</a>
<? } ?>
</td> <td width="*">
<?
if($_SESSION['Functie']=="admin") {
    switch(
$_GET['item']) {
        case
'ledenoverzicht';
        include(
'includes/admin/ledenoverzicht.php');
        break;
        case
'ledentoevoegen';
        include(
'includes/admin/ledentoevoegen.php');
        break;
    }
}
?>
</body>
</html>
<?
} else {
header("location: Login3.php");
}
?>

<?
$query
= "SELECT * FROM tblGebruikers";
$result=mysql_query($query);
$veldnaam = mysql_field_name($result,1);

$Aantalrijen=mysql_numrows($result);
$Aantalvelden = mysql_num_fields($result);
mysql_close();

echo
"<table border='1'>";
echo
"<tr>";
    for(
$kolom=0;$kolom<=$Aantalvelden-1;$kolom++)
{
$naamveld=mysql_field_name($result,$kolom);
        echo
"<td>" . $naamveld . "</td>";
    }
echo
"<td>Bewerkingen</td>";
echo
"</tr>";

for(
$rij=0;$rij<=$Aantalrijen-1;$rij++) {
    echo
"<tr>";
    for(
$kolom=0;$kolom<=$Aantalvelden-1;$kolom++)
{
       
$waardeveld=mysql_result($result,$rij,$kolom);
        echo
"<td>" . $waardeveld . "</td>";
    }
   
$id = mysql_result($result,$rij,'gebruikerid');
    echo
"<td>
    <a href='hoofdpagina.php?item=ledenoverzicht&actie=
verwijderen&id=$id'>verwijderen</a><br>
    <a href='?item=lidaanpassen&id=$id'>aanpassen</a>
    echo "
</td>";
    echo "
</tr>";
}
echo "
</table>";
?>

<?
include("
includes/connect.php");
if($_POST['btnAanpassen'] == 'Aanpassen') {
    $voornaam = $_POST['txtVoornaam'];
    $naam = $_POST['txtNaam'];
    $gebruikersnaam = $_POST['txtGebruikersnaam'];
    $paswoord = $_POST['txtPaswoord'];
    $functie = $_POST['cboFunctie'];
    $aantepassenid = $_GET['id'];
   
    $query="
UPDATE `tblgebruikers` SET `naam` = '$naam', `voornaam` = '$voornaam', `gebruikersnaam` =
 
'$gebruikersnaam', `wachtwoord` = '$paswoord', `functie` = '$functie' WHERE `gebruikerid` =$aantepassenid";
mysql_query($query);
}
?>

<?
if($_GET['actie'] == "
verwijderen") {
    $idverwijderen = $_GET['id'];
    $query = "
DELETE FROM `tblgebruikers` WHERE `gebruikerid` = $idverwijderen";
    $result=mysql_query($query);
}
?>

<?
include("
includes/connect.php");
if($_GET['actie']=='toevoegen' and isset($_POST['btnToevoegen'])) {
    $naam= $_POST['txtNaam'];
    $gebruikersnaam= $_POST['txtGebruikersnaam'];
    $wachtwoord= $_POST['txtWachtwoord'];
    $query="
INSERT INTO `tblgebruikers`
( `
gebruikerid` , `naam` , `voornaam` , `straatennummer` , `postcode` , `gemeente` , `email` , `telefoon` , `geslacht` , `gebdatum` , `gebruikersnaam` , `wachtwoord` , `klas` , `functie` )
   
VALUES ('', '".$naam."', '', '', '', '', '', '', '', '0000-00-00', '".$gebruikersnaam."', '".$wachtwoord."', '', '')";
    mysql_query($query);
    echo $naam . "
is toegevoegd";
}

$query = "
SELECT * FROM tblGebruikers";
$result=mysql_query($query);
$veldnaam = mysql_field_name($result,1);
?>
kendsnyder+phpnet at gmail dot com
02-Jun-2007 05:08
The only way to convert a large float to a string is to use printf('%0.0f',$float); instead of strval($float); (php 5.1.4).

// strval() will lose digits around pow(2,45);
echo pow(2,50); // 1.1258999068426E+015
echo (string)pow(2,50); // 1.1258999068426E+015
echo strval(pow(2,50)); // 1.1258999068426E+015

// full conversion
printf('%0.0f',pow(2,50)); // 112589906846624
echo sprintf('%0.0f',pow(2,50)); // 112589906846624
portos_ze_retour at hotmail dot fr
10-Mar-2006 04:15
In complement to Tom Nicholson's contribution, here is the french version (actually it's possible to change the language, but you should check the syntax ;) )

function int_to_words($x) {
   global $nwords;

   if(!is_numeric($x))
     $w = '#';
   else if(fmod($x, 1) != 0)
     $w = '#';
   else {
     if($x < 0) {
         $w = $nwords['minus'].' ';
         $x = -$x;
     } else
         $w = '';
     // ... now $x is a non-negative integer.

     if($x < 21)  // 0 to 20
         $w .= $nwords[$x];
     else if($x < 100) {  // 21 to 99
         $w .= $nwords[10 * floor($x/10)];
         $r = fmod($x, 10);
         if($r > 0)
           $w .= '-'. $nwords[$r];
     } else if($x < 1000) {  // 100 to 999
         $w .= $nwords[floor($x/100)] .' '.$nwords['hundred'];
         $r = fmod($x, 100);
         if($r > 0)
           $w .= ' '.$nwords['separator'].' '. int_to_words($r);
     } else if($x < 1000000) {  // 1000 to 999999
         $w .= int_to_words(floor($x/1000)) .' '.$nwords['thousand'];
         $r = fmod($x, 1000);
         if($r > 0) {
           $w .= ' ';
           if($r < 100)
               $w .= $nwords['separator'].' ';
           $w .= int_to_words($r);

         }
     } else {    //  millions
         $w .= int_to_words(floor($x/1000000)) .' '.$nwords['million'];
         $r = fmod($x, 1000000);
         if($r > 0) {
           $w .= ' ';
           if($r < 100)
               $word .= $nwords['separator'].' ';
           $w .= int_to_words($r);
         }
     }
   }
   return $w;
}

 // Usage in English
$nwords = array( "zero", "one", "two", "three", "four", "five", "six", "seven",
                   "eight", "nine", "ten", "eleven", "twelve", "thirteen",
                   "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
                   "nineteen", "twenty", 30 => "thirty", 40 => "forty",
                   50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty",
                   90 => "ninety" , "hundred" => "hundred", "thousand"=> "thousand", "million"=>"million",
                   "separator"=>"and", "minus"=>"minus");

echo 'There are currently '. int_to_words(-120223456) . ' members logged on.<br>';

 //Utilisation en Francais
$nwords = array( "zéro", "un", "deux", "trois", "quatre", "cinq", "six", "sept",
                   "huit", "neuf", "dix", "onze", "douze", "treize",
                   "quatorze", "quinze", "seize", "dix-sept", "dix-huit",
                   "dix-neuf", "vingt", 30 => "trente", 40 => "quarante",
                   50 => "cinquante", 60 => "soixante", 70 => "soixante-dix", 80 => "quatre-vingt",
                   90 => "quatre-vingt-dix" , "hundred" => "cent", "thousand"=> "mille", "million"=>"million",
                   "separator"=>"", "minus"=>"moins");

echo 'Il y a actuellement '. int_to_words(-120223456) . ' membres connectés.<br>';
anthony dot parsons at manx dot net
09-Jan-2006 05:59
If you have to compare object variables like this be careful not to make a typo, or you could end up calling __set() -
<?php
/* Does what you'd expect it to */
if ( $user->password == $user2->password )

/* Doesn't */
if ( $user->password = $user2->password )
?>

To avoid that ever happening, do it like this:
<?php
if ( strval($user->password) == $user2->password )
?>
php at ianco dot co dot uk
07-Oct-2005 11:36
I can't help being surprised that

(string)"0" == (string)"0.00"

evaluates to true. It's the same with strval and single quotes.
=== avoids it.

Why does it matter? One of my suppliers, unbelievably, uses 0 to mean standard discount and 0.00 to mean no discount in their stock files.
Steve Ball
09-Sep-2005 02:18
It seems that one is being treated as an unsigned large int (32 bit), and the other as a signed large int (which has rolled over/under).

2326201276 - (-1968766020) =  4294967296.
brettsg at serialio dot com
02-Aug-2005 04:07
How come this code in version 4.4 does something different than in version 4.3?

$val = 538759009 ^ 0xAABBCCDD;
print "val=" . $val;

= 2326201276 (version 4.4)

and

= -1968766020 (version 4.3)
Redbeard
24-Aug-2004 09:40
You can also use the PEAR package Numbers_Words, which handles many other languages.
Tom Nicholson
28-Apr-2004 04:13
If you want to convert an integer into an English word string, eg. 29 -> twenty-nine, then here's a function to do it.

Note on use of fmod()
  I used the floating point fmod() in preference to the % operator, because % converts the operands to int, corrupting values outside of the range [-2147483648, 2147483647]

I haven't bothered with "billion" because the word means 10e9 or 10e12 depending who you ask.

The function returns '#' if the argument does not represent a whole number.

<?php
$nwords
= array( "zero", "one", "two", "three", "four", "five", "six", "seven",
                  
"eight", "nine", "ten", "eleven", "twelve", "thirteen",
                  
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
                  
"nineteen", "twenty", 30 => "thirty", 40 => "forty",
                  
50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty",
                  
90 => "ninety" );

function
int_to_words($x) {
   global
$nwords;

   if(!
is_numeric($x))
     
$w = '#';
   else if(
fmod($x, 1) != 0)
     
$w = '#';
   else {
      if(
$x < 0) {
        
$w = 'minus ';
        
$x = -$x;
      } else
        
$w = '';
     
// ... now $x is a non-negative integer.

     
if($x < 21)   // 0 to 20
        
$w .= $nwords[$x];
      else if(
$x < 100) {   // 21 to 99
        
$w .= $nwords[10 * floor($x/10)];
        
$r = fmod($x, 10);
         if(
$r > 0)
           
$w .= '-'. $nwords[$r];
      } else if(
$x < 1000) {   // 100 to 999
        
$w .= $nwords[floor($x/100)] .' hundred';
        
$r = fmod($x, 100);
         if(
$r > 0)
           
$w .= ' and '. int_to_words($r);
      } else if(
$x < 1000000) {   // 1000 to 999999
        
$w .= int_to_words(floor($x/1000)) .' thousand';
        
$r = fmod($x, 1000);
         if(
$r > 0) {
           
$w .= ' ';
            if(
$r < 100)
              
$w .= 'and ';
           
$w .= int_to_words($r);
         }
      } else {   
//  millions
        
$w .= int_to_words(floor($x/1000000)) .' million';
        
$r = fmod($x, 1000000);
         if(
$r > 0) {
           
$w .= ' ';
            if(
$r < 100)
              
$word .= 'and ';
           
$w .= int_to_words($r);
         }
      }
   }
   return
$w;
}

?>

Usage:
<?php
echo 'There are currently '. int_to_words($count) . ' members logged on.';
?>

unserialize> <settype
Last updated: Fri, 01 Jun 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites