How to echo random text in PHP? [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have about 300 lines of text that need to be echoed randomly.
Here's my current code:
<?php
$lines = array(
'Line 1',
'Line 2',
'Line 3'
);
$powered = echo $lines[rand(0, count($lines)-1)];
echo $powered;
?>
I know that the issue is probably with line 9 because I'm not sure how to assign the echoing to a variable. I need to be able to include this file in several others and echo $powered to get a random line.
Any help would be really appreciated.

$lines = array("one", "two", "three","four");
for($i =0; i < count($lines)-1;i++){
$line = $lines[rand(0, count($lines)-1)];
$lines = array_diff($lines, array($line));//Use this to remove the index of array
}
i wrote this in the stack overflow chat so there might be a problem or two. though, all looks well to me.
Was this what you were looking for?

Related

how to add string to variable in php? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
i just need on small info i want just add '#' to one variable and and put add thing into one variable. i am adding small php code to here please suggest me.
<?php
$email34 = $row['email'];
$rem = '#gmail.com';
$trim_email = str_replace($rem ,'', $email34);
$tag_name ="#".$trim_emial.;
echo $tag_name;
?>
but i am getting only # as output;
but my out should be "#mahesh1" if any have idea about this code please help me. thank you advanced.
there is so much spelling mistakes in the variables... try below given code
<?php $email34 = $row['email'];
$rem = '#gmail.com';
$trim_email = str_replace($rem ,'', $email34);
$tag_name ="#".$trim_email;
echo $tag_name;
?>

generating number between 1 to million syntax error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This code
<? php
$randdnumber = rand(1, 1000000);
echo "you won ".$randnumber."points";
?>
gives me error
syntax error, unexpected '$randdnumber' (T_VARIABLE) in index.php
also how to add output of two function together something like ..
You won [$randdnumber] also you won [$randdnumber2]
is it possible ?
FIXED CODE
<?php
$randdnumber = rand(1, 1000000);
echo "you won ".$randdnumber."points";
?>
fix php tags and changed variable name
FIXED
<?php
$randnumber = rand(1, 1000000);
echo "you won ".$randnumber."points";
?>
The exact problem is your php open tag is having space and also variable name you assigned and echoed are different :
Please remove it and change it to
<?php
$randdnumber = rand(1, 1000000);
echo "you won ".$randdnumber."points";
?>

PHP String Replace for BBCode [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to make a custom BBCode for my forum site, but I've run into an issue, and I'm having a hard time fixing it.
This is what's in the database for the body of the thread "[b]Bold[/b][i]Italic[/i][strike]Strike[/strike]".
However the output is displayed like this "[i]Italic[/i][strike]Strike[/strike]".
So, I'm guessing it's an issue with echoing it out, but i'm not sure how to fix it. Here's the current code:
function bbcode($input) {
$input = strip_tags($input);
$input = htmlentities($input);
$search = array('/\[b\](.*?)\[\/b\]/is');
$replace = array('<b>$body</b>');
return preg_replace($search, $preg_replace, $input);
}
while($row = mysql_fetch_array($threadquery, MYSQL_ASSOC)) {
$body = str_replace("\n",'<br>', $row['body']);
}
echo bbcode($body);
proper code should be:
$replace = array('<b>$1</b>');
return preg_replace($search, $replace, $input);

String concatenation and JSON value cause error in PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The title is a bit confusing, but the error which I have is -
$xml="<Contacts>";
for($i=0;$i<count($results['records']);$i++){
$xml. = "<Contact>
<Name>".$results['records'][$i]['name']."</Name>";
}
$xml.="</Contacts>";
When I try to add something to string (concatenate) I get 500 Internal server error. I believe that problem is in " $results['records'][$i]['name']". I think the solution is to replace JSON value with variable and enclose it in {}...maybe I am wrong, i don't know.
UPD:
if I "echo ".$results['records'][$i]['name'].""; It works fine.
You have a string concatenation (.=) syntax error. Change
$xml. = "<Contact>
to
$xml .= "<Contact>
I'm strictly answering the question about the "string concatenation" PHP error. No downvotes for anything except this point, please. But yes, try not to generate XML manually. I closed </Contact>s for you below.
$xml="<Contacts>";
for ($i = 0; $i < count($results['records']); $i++) {
$xml .= "<Contact><Name>".$results['records'][$i]['name']."</Name></Contact>";
}
$xml.="</Contacts>";

PHP console does not echo [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Okay this really strange or I am missing something. When I run this very simmple PHP script on cmd I get the expected output which is 0. but when I uncomment the last two lines of code. . .nothing is displayed.
<?php
$test1 = 0;
echo $test1;
$test2 = 0;
#$test1_weight = 0:
#$test2_weight = 0;
?>
Is there some rule against declaring variables after an echo statement?
$test1_weight = 0:
--> change ":" to ";"
No there is no rule against declaring variables after an echo statement

Categories