Frank P Posted March 21, 2013 Share Posted March 21, 2013 Depending on a set value in the URL, the html element should get a class. In this way it works: include-test.php: <?php require("include-test-included.php"); ?> <!DOCTYPE HTML> <html class="<?php echo makeClass(); ?>"> <head> <meta charset="utf-8"> <title>Demo</title> <style> html.normal { background: white; } html.touch { background: red; } </style> </head> <body> </body> </html> include-test-included.php: <?php function makeClass() { if(!isset($_GET['type'])) { $type = 'normal'; } elseif (isset($_GET['type'])) { $type = $_GET['type']; } return $type; } ?> But I would very much like to be able to do it this way, because I have a number of pages, and the head section is already largely PHP-generated: include-test.php: <?php require("include-test-included.php"); ?> <?php echo $part_1 ?> <title>Demo</title> <style> html.normal { background: white; } html.touch { background: red; } </style> </head> <body> </body> </html> include-test-included.php: <?php function makeClass() { if(!isset($_GET['type'])) { $type = 'normal'; } elseif (isset($_GET['type'])) { $type = $_GET['type']; } return $type; } $part_1 = '<!DOCTYPE HTML> <html class="<?php echo $type; ?>"> <head> <meta charset="utf-8"> ' ?> However, this way I get an undefined-variable warning. Which makes sense, because it is outside the function. But how do I get this class variable in the html tag? I have been fiddling for a couple of hours already with backslashes, single/double quotes and echoing the function, but have not found the right way yet. Which is the right way? Link to comment https://forums.phpfreaks.com/topic/275968-how-to-get-this-class-variable-in-this-tag/ Share on other sites More sharing options...
haku Posted March 21, 2013 Share Posted March 21, 2013 What is the variable that is undefined? Link to comment https://forums.phpfreaks.com/topic/275968-how-to-get-this-class-variable-in-this-tag/#findComment-1420101 Share on other sites More sharing options...
Frank P Posted March 21, 2013 Author Share Posted March 21, 2013 Thanks for jumping so quickly! I was mistaking, I got the undefined-variable warning in another configuration. I don't know which one again, because I have tried so many combinations. This is what I get as a result in the above (latter) configuration: a literal echoing of the line: <html class="<?php echo $type; ?>"> Link to comment https://forums.phpfreaks.com/topic/275968-how-to-get-this-class-variable-in-this-tag/#findComment-1420104 Share on other sites More sharing options...
haku Posted March 21, 2013 Share Posted March 21, 2013 That's because you are echoing the PHP tags. You are already in PHP, you don't need php tags. Link to comment https://forums.phpfreaks.com/topic/275968-how-to-get-this-class-variable-in-this-tag/#findComment-1420106 Share on other sites More sharing options...
Frank P Posted March 21, 2013 Author Share Posted March 21, 2013 You mean like this: <?php function makeClass() { if(!isset($_GET['type'])) { $type = 'normal'; } elseif (isset($_GET['type'])) { $type = $_GET['type']; } return $type; } $part_1 = '<!DOCTYPE HTML> <html class=\'echo $type\'> <head> <meta charset="utf-8"> ' or: <?php function makeClass() { if(!isset($_GET['type'])) { $type = 'normal'; } elseif (isset($_GET['type'])) { $type = $_GET['type']; } return $type; } $part_1 = '<!DOCTYPE HTML> <html class="echo $type"> <head> <meta charset="utf-8"> ' ? Unfortunately, that doesn't work either. In both cases I still get a literal echoing of $part_1 as a result. No class="normal" or class="touch". Link to comment https://forums.phpfreaks.com/topic/275968-how-to-get-this-class-variable-in-this-tag/#findComment-1420111 Share on other sites More sharing options...
haku Posted March 21, 2013 Share Posted March 21, 2013 You are trying to echo inside an echo. That won't work. You need to do it like this: $name = 'haku'; echo 'My name is ' . $name . ', and this is a string'; Link to comment https://forums.phpfreaks.com/topic/275968-how-to-get-this-class-variable-in-this-tag/#findComment-1420112 Share on other sites More sharing options...
Frank P Posted March 21, 2013 Author Share Posted March 21, 2013 Got it! I saw your example and suddenly realized that the + in javascript, with which I primarily work, is not a + but a . in PHP! This is how it works the way I want it to: <?php function makeClass() { if(!isset($_GET['type'])) { $type = 'normal'; } elseif (isset($_GET['type'])) { $type = $_GET['type']; } return $type; } $part_1 = '<!DOCTYPE HTML> <html class="'.makeClass().'"> <head> <meta charset="utf-8"> ' ?> Again, thanks for jumping in so quickly! Link to comment https://forums.phpfreaks.com/topic/275968-how-to-get-this-class-variable-in-this-tag/#findComment-1420123 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.