php: variables in strings [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to use strings that contains variables that have to be interpreted after the definition of the string.
$mystring = "aaa".{$i};
...
$i=3;
echo $mystring; //Expected result: aaa3
Is it possible to load into $mystring not the value of the variable but only the name of the variable, so that its value can be loaded later?
Thanks for help.

This is a confusing question. If you want the result to be AAA3, then the code should be:
$i=3;
$com="AAA";
echo $variable = $com.$i;

Related

how do I get just one value from mysql php query results [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Below are the results of my query. I would just like to return the refundable_amount value. Which for this query $4.04
a:1:{i:0;a:19:{s:8:"label_id";i:3324543;s:8:"tracking";s:22:"9400136205309684548265";s:17:**"refundable_amount";d:4.04**;s:7:"created";i:1614781854687;s:10:"carrier_id";s:4:"usps";s:12:"service_name";s:23:"USPS - First Class Mail";s:6:"status";s:9:"PURCHASED";s:22:"commercial_invoice_url";N;s:12:"package_name";s:11:"Single Kcup";s:9:"is_letter";b:0;s:13:"product_names";a:1:{i:0;s:28:"Multiple Aware";}s:11:"product_ids";a:1:{i:0;i:1212;}s:15:"receipt_item_id";i:72845982;s:12:"created_date";i:1614781859000;s:11:"expiry_date";i:1630333858000;s:15:"main_receipt_id";i:54663164;s:4:"rate";d:4.04;s:8:"currency";s:3:"USD";s:12:"label_cached";i:1614781863000;}}
Thank you in advance for any help.
You have typo, in near the "Multiple Aware" output, it should be 14, if you correct it like below,
Then you can use php unserialize function
$text = unserialize ('a:1:{i:0;a:19:{s:8:"label_id";i:3324543;s:8:"tracking";s:22:"9400136205309684548265";s:17:"refundable_amount";d:4.04;s:7:"created";i:1614781854687;s:10:"carrier_id";s:4:"usps";s:12:"service_name";s:23:"USPS - First Class Mail";s:6:"status";s:9:"PURCHASED";s:22:"commercial_invoice_url";N;s:12:"package_name";s:11:"Single Kcup";s:9:"is_letter";b:0;s:13:"product_names";a:1:{i:0;s:14:"Multiple Aware";}s:11:"product_ids";a:1:{i:0;i:1212;}s:15:"receipt_item_id";i:72845982;s:12:"created_date";i:1614781859000;s:11:"expiry_date";i:1630333858000;s:15:"main_receipt_id";i:54663164;s:4:"rate";d:4.04;s:8:"currency";s:3:"USD";s:12:"label_cached";i:1614781863000;}}');
you can get the value as below,
echo $text[0]['rate'];
will output
4.04
For more information about this function you can visit
https://www.php.net/manual/en/function.unserialize.php

how do I select one word from array list like this one? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this
$words = array("Car", "Dog", "Ship");
now I want to randomly select only one word, please help!
This could work.
$words[rand(0, sizeof($words) - 1)]
You can use array_rand() function to get more than one random index.
array_rand(array, number)
<?php
$words=array("Car", "Dog", "Ship");
$random_keys=array_rand($words,3);
echo $words[$random_keys[0]]."<br>";
echo $words[$random_keys[1]]."<br>";
echo $words[$random_keys[2]];
?>

how to create dynamic string in php like sample? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
hi i want to create string in laravel with foreach and that string must be like this sample:
'item1,item2,item3'
Pay attention to commas and quotation marks.
anyone can help me?
you can use implode function.
Have a look at https://www.php.net/implode
It does exactly that with arrays.
example:
$string = implode(",", $array);

How to detect a newline with space [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
my string is "mom dad" (also saves the same attribute in table db).
After I query and print to the table, I want to separate a newline with a space. The output should be mom+newline+dad, but it prints "mom dad" without a new line.
Use PHP explode() function like this :
<?php
$input = "mom dad";
$inputArray = explode(" ", $input);
//now inputArray is like this ["mom", "dad", ...]
echo $inputArray[0]."<br>".$inputArray[1];
?>
Hope it helps
Thanks

$_GET inside of case [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
so I would like to use something like /action=productSpec?product=1
And then echo the value "1".
I have this:
case "productSpec":
$show = $_GET['product']
echo $show;
break;
But it doesn't work.
Why? Thank you.
"Case is also GET";
Change that part in the URL to ?action=productSpec&product=1

Categories