Jump to content

Recommended Posts

Ok, basically had my first class of php yesterday. Got this as part of an assignment:

 

"For this part of the exercise you must use the rand() function to generate a number

between 10 and 30. You should then print the words “This line is in font‐size x px” where x

is the number generated by the rand() function. The words should be displayed in the

relevant font size."

 

I can get my sentence to display the random number, but for the life of me i cant change the actual font size. Whats wrong with my code?:

 

$font=rand(1,30);

 

 

print "<div style='font-size:$fontpx';>This line is in font size $font px</div>";

 

 

Link to comment
https://forums.phpfreaks.com/topic/144795-gotta-give-this-in-tommorow-help-a-noob/
Share on other sites

encapsulate $font in the string with {$font} and it will work.  The string thinks you are trying to call "$fontpx" is is not a variable.

 

Also note, that if it is 10 to 30, you have a 1 instead of 10 in your rand function.

PHP will evaluate variables inside double quoted strings, but it sees your first variable as $fontpx not $font followed by the characters p and x. You need to either stop/start the string:

print "<div style='font-size:".$font."px';>This line is in font size $font px</div>";

or use curly braces to show php where the variable start/stops:

print "<div style='font-size:{$font}px';>This line is in font size $font px</div>";

 

read up on it here:

http://us.php.net/manual/en/language.types.string.php#language.types.string.parsing

encapsulate $font in the string with {$font} and it will work.  The string thinks you are trying to call "$fontpx" is is not a variable.

 

Also note, that if it is 10 to 30, you have a 1 instead of 10 in your rand function.

 

I think, i think i love you!

 

I tried normal brackets, but didn't think of {}

 

Thx!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.