Asked  2 Years ago    Answers:  5   Viewed   87 times

Is there a way to convert an integer to a string in PHP?



You can use the strval() function to convert a number to a string.

From a maintenance perspective its obvious what you are trying to do rather than some of the other more esoteric answers. Of course, it depends on your context.

$var = 5;

// Inline variable parsing
echo "I'd like {$var} waffles"; // = I'd like 5 waffles

// String concatenation 
echo "I'd like ".$var." waffles"; // I'd like 5 waffles

// The two examples above have the same end value...
// ... And so do the two below

// Explicit cast 
$items = (string)$var; // $items === "5";

// Function call
$items = strval($var); // $items === "5";
Friday, September 23, 2022

It depends on the driver used between php and mysql.

Check which one of them is used by checking pdo_mysql section of the output of

php -i

your output should be similar to


PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $

The native driver return integers as integers, but the other return them as strings.

So the solution is to remove the old driver and install the native one.

or to use $casts into your model.

    protected $casts = [
    'status' => 'integer',
Sunday, August 14, 2022

CStr(45) is all you need (the Convert String function)

Sunday, August 7, 2022

This is either a bug in TypeScript or a concious design decision, but you can work around it using:

var myBool: bool = true;
var myString: string = String(myBool);

In JavaScript booleans override the toString method, which is available on any Object (pretty much everything in JavaScript inherits from Object), so...

var myString: string = myBool.toString();

... should probably be valid.

There is also another work around for this, but I personally find it a bit nasty:

var myBool: bool = true;
var myString: string = <string><any> myBool;
Friday, December 23, 2022

After analyzing your riddle, I think this is what you want:

$one = $_REQUEST["term"] . "%";

This appends the % sql placeholder to that search term. (That's what I surmise here.)

Sunday, December 25, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages