How to do make a string in quotes from integer in better way in php?
$number = 1;
$number = "'" . $number . "'";
result is '1', which is good, but can it be done more nicely? I tried (string) $number but result was just 1 not '1'.
$num = 1221;
echo $numStr = "'$num'";
Results: '1221'
You can also try:
$num = 1221;
echo $numStr = "'$num'"; // output: '1221'
var_dump($strVal); // output: string(4) "1221"
Related
I have read through many other posts about this non-numeric value encountered error, but unfortunately, I may be dense. I'm not sure how to fix this annoying error.
Here is my code block:
foreach ($local_words as $local_word_to_check){
$letters = mb_str_split($local_word_to_check);
$letters = array_map( 'addslashes', $letters );
$local_total_letter_box_length = "";
$query2 = "SELECT GROUP_CONCAT(Alpha) AS Alpha,
GROUP_CONCAT(Letter_Box_Width) AS Letter_Box_Width
FROM Font_Krinkes
WHERE Alpha IN ('" . implode("','", $letters) . "')";
$result2 = mysqli_query($con, $query2) or die("unable to query database!");
if ($row = mysqli_fetch_assoc($result2)) {
$widths = array_combine(explode(',', $row['Alpha']), explode(',', $row['Letter_Box_Width']));
$total_word_box_width = 0;
foreach (mb_str_split($local_word_to_check) as $letter) {
$local_total_letter_box_length += $widths[$letter];
}
}
$complete_font_values[] = $local_total_letter_box_length;
unset($letters);
}
The error happens on this line:
$local_total_letter_box_length += $widths[$letter];
Any help is very much appreciated.
$local_total_letter_box_length = ""; is a string when it is first declared.
$local_total_letter_box_length += $widths[$letter]
That's not how you concatenate a string in php.
Use .= to concatenate.
If you mean to increase a numeric value, declare your variable with 0.
+= is okay to use on numeric values and arrays.
Code: (Demo)
$integer = 5;
$integer += 10;
echo $integer , "\n---\n";
$array = ['good' => 'one'];
$array += ['two'];
var_export($array);
Output:
15
---
array (
'good' => 'one',
0 => 'two',
)
Use
$local_total_letter_box_length = 0;
Instead of
$local_total_letter_box_length = "";
Most of time, this error happens when used "+" as strings concatenation operator like other languages, but in PHP strings concatenation operator is ".", if you have numberic value as string you have to cast it like $num = (int) "10" + (int) "20";
I have array that contain 7 string value.
I want to store it in different variable.
but i have 10 variable so i wish that, the variable not contain any array value that's value is zero.
<?php
$str = ("my", "name" , "ajay "," and "," i "," am ", " vageration " );
?>
i want to get this string value like
$a1=
$a1=my
$a3=name
$a4=ajay
$a5=and
$a6=i
$a7=am
$a8=vageration
$a9=0
$a10=0
i don`t know how can i print this value
I think I wont answer to your question, but I don't really understand what you want.
If you want to dynamically assign variables in php, I could invite you to do something like that:
<?php
$str = ("my", "name" , "ajay "," and "," i "," am ", " vageration " );
for ($i = 0; $i < 10; $i++)
${'a'.$i} = (isset ($str[$i]) ? $str[$i] : 0);
?>
In your case, this will create the following variables :
<?php
$a0 = "my";
$a1 = "name";
$a2 = "ajay ";
$a3 = " and ";
$a4 = " i ";
$a5 = " am ";
$a6 = " vageration ";
$a7 = 0;
$a8 = 0;
$a9 = 0;
?>
This line (${'a'.$i} = (isset ($str[$i]) ? $str[$i] : 0) uses the ternary operator, which means that the variable ${'a'.$i} will have $str[$i]'s value if such a value exists or 0 otherwise.
Not sure it helps. I'll edit or delete my message if necessary.
I need some help. I have "name" and "id", I've display is as "id"-"name" (suggestion) I have a function that convert it to "name-id" (displayed in textbox after picking the suggestion).
The problem is, how can I properly explode if the name = Backlink-Spider and id = 25, the result
will be 25-Backlink-Spider and converted it to Backlink-Spider-25 . I tend to return it to unconverted , the result is Spider-25-Backlink.
This is my code.
$xpldName = explode("-", $posted['name'], 2);
$cdata = $C->loadByName($xpldName[1]);
$_POST['name'] = $xpldName[1]."-".$xpldName[0];
Explode the whole string.
Implode all the strings except the last one
Print last string - imploded string
Demo Code(Not tested):
$xpldName = explode("-", $posted['name'], 2);
$name = array_slice($xpldName, 0, -1);
$name = implode("-", $name);
$id = end($xpldName);
echo $id . " " . $name;
I have this two digit number i.e. 50/20 which is seperated by slash and stored in one column of database.
$value = '50/20';
I want to get separate number as
$num1 = 50;
$num2 = 20;
and sum as
$sum = $num1+$num2;
Is there any solution for to separate those combine numbers.
Use explode
Try like this
$value = '50/20';
$arr = explode('/',$value);
$sum = $arr[0]+$arr[1];
//Output
$arr[0] contains 50
$arr[1] contains 20
You can check this by simply doing print_r($arr);
You can do also something like:
$value = '50/20';
$sum = array_sum(explode('/', $value));
echo $sum; // 70
$exp = explode('/',$value);
$value1 = $exp[0];
$value2 = $exp[1];
$sum = $value1 + $value2;
$value = '50/20';
$arr = explode('/',$value);
$num1=$arr[0];
$num2=$arr[1];
$sum=$num1+$num2;
print_r($sum);
Output
70
Explode Function is used to convert a sting to array.
This should work for you.
$value = "50/20";
$numbers = preg_split("/\//",$value);
$sum = $numbers[0]+$numbers[1];
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";
There's many ways to do this.
Two examples:
$str = (string) $int;
$str = "$int";
See the PHP Manual on Types Juggling for more.
$foo = 5;
$foo = $foo . "";
Now $foo is a string.
But, you may want to get used to casting. As casting is the proper way to accomplish something of that sort:
$foo = 5;
$foo = (string)$foo;
Another way is to encapsulate in quotes:
$foo = 5;
$foo = "$foo"
There are a number of ways to "convert" an integer to a string in PHP.
The traditional computer science way would be to cast the variable as a string:
$int = 5;
$int_as_string = (string) $int;
echo $int . ' is a '. gettype($int) . "\n";
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
You could also take advantage of PHP's implicit type conversion and string interpolation:
$int = 5;
echo $int . ' is a '. gettype($int) . "\n";
$int_as_string = "$int";
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
$string_int = $int.'';
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
Finally, similar to the above, any function that accepts and returns a string could be used to convert and integer. Consider the following:
$int = 5;
echo $int . ' is a '. gettype($int) . "\n";
$int_as_string = trim($int);
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
I wouldn't recommend the final option, but I've seen code in the wild that relied on this behavior, so thought I'd pass it along.
Use:
$intValue = 1;
$string = sprintf('%d', $intValue);
Or it could be:
$string = (string)$intValue;
Or:
settype($intValue, 'string');
Warning: the below answer is based on the wrong premise. Casting 0 number to string always returns string "0", making the code provided redundant.
All these answers are great, but they all return you an empty string if the value is zero.
Try the following:
$v = 0;
$s = (string)$v ? (string)$v : "0";
There are many possible conversion ways:
$input => 123
sprintf('%d',$input) => 123
(string)$input => 123
strval($input) => 123
settype($input, "string") => 123
You can either use the period operator and concatenate a string to it (and it will be type casted to a string):
$integer = 93;
$stringedInt = $integer . "";
Or, more correctly, you can just type cast the integer to a string:
$integer = 93;
$stringedInt = (string) $integer;
As the answers here demonstrates nicely, yes, there are several ways. However, in PHP you rarely actually need to do that. The "dogmatic way" to write PHP is to rely on the language's loose typing system, which will transparently coerce the type as needed. For integer values, this is usually without trouble. You should be very careful with floating point values, though.
I would say it depends on the context. strval() or the casting operator (string) could be used. However, in most cases PHP will decide what's good for you if, for example, you use it with echo or printf...
One small note: die() needs a string and won't show any int :)
$amount = 2351.25;
$str_amount = "2351.25";
$strCorrectAmount = "$amount";
echo gettype($strCorrectAmount); //string
So the echo will be return string.
My situation :
echo strval("12"); => 12
echo strval("0"); => "0"
I'm working ...
$a = "12";
$b = "0";
echo $a * 1; => 12
echo $b * 1; => 0
I tried all the methods above yet I got "array to string conversion" error when I embedded the value in another string. If you have the same problem with me try the implode() function.
example:
$integer = 0;
$id = implode($integer);
$text = "Your user ID is: ".$id ;
You can simply use the following:
$intVal = 5;
$strVal = trim($intVal);
$integer = 93;
$stringedInt = $integer.'';
is faster than
$integer = 93;
$stringedInt = $integer."";