zartzar Posted February 11, 2009 Share Posted February 11, 2009 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/144795-gotta-give-this-in-tommorow-help-a-noob/ Share on other sites More sharing options...
premiso Posted February 11, 2009 Share Posted February 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144795-gotta-give-this-in-tommorow-help-a-noob/#findComment-759796 Share on other sites More sharing options...
rhodesa Posted February 11, 2009 Share Posted February 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/144795-gotta-give-this-in-tommorow-help-a-noob/#findComment-759798 Share on other sites More sharing options...
zartzar Posted February 11, 2009 Author Share Posted February 11, 2009 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!! Quote Link to comment https://forums.phpfreaks.com/topic/144795-gotta-give-this-in-tommorow-help-a-noob/#findComment-759799 Share on other sites More sharing options...
killah Posted February 11, 2009 Share Posted February 11, 2009 echo '<div style="font-size: '.rand(10,30).'px;">This font size is undefined?</div>'; Quote Link to comment https://forums.phpfreaks.com/topic/144795-gotta-give-this-in-tommorow-help-a-noob/#findComment-759818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.