I’ve been tinkering with the echo statement to make my logout button print in my header (when a user is logged in) but I keep either getting errors of this nature:
Parse error: syntax error, unexpected T_ECHO, expecting ‘,’ or ‘;’ in /vagrant/web/Project/application/views/_header.php on line 19 Call Stack: 0.0013 634608 1. {main}() /vagrant/web/Project/index.php:0 0.0123 636728 2. require_once(‘/vagrant/web/Project/system/core/CodeIgniter.php’) /vagrant/web/Project/index.php:202 0.1486 1025392 3. call_user_func_array() /vagrant/web/Project/system/core/CodeIgniter.php:359 0.1486 1025528 4. Fp_controller->index() /vagrant/web/Project/system/core/CodeIgniter.php:0 0.1486 1025976 5. Viewlib->loadview() /vagrant/web/Project/application/controllers/fp_controller.php:12 0.1487 1026192 6. CI_Loader->view() /vagrant/web/Project/application/libraries/Viewlib.php:16 0.1487 1027600 7. CI_Loader->_ci_load() /vagrant/web/Project/system/core/Loader.php:419
or the url will just be completely screwed up (it will literally have “base_url();” instead of the actual base url name that I declared in my config file. Could somebody help me?
Header view
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Final Project Incorporated</title>
</head>
<body>
<h1>Navigation:</h1>
<ul>
<li><a href="<?php echo base_url(); ?>fp_controller/index">Home</a></li>
<li><a href="<?php echo base_url(); ?>fp_controller/directory">Employee Directory</a></li>
<li><a href="<?php echo base_url(); ?>fp_controller/form">Add Employee Form</a></li>
<li><a href="<?php echo base_url(); ?>fp_controller/login">Login</a></li>
<?php
if ($this->session->userdata('isLoggedIn')) {
Line 19>>>> echo "<li>" . "<a href=" . "'" echo base_url();"'" . "fp_controller/logout" . ">" . "Logout" . "</a>" . "</li>";
}
?>
</ul>
<hr />
</body>
</html>
You can’t put an echo inside a php statement, just concatenate:
You could also use site_url() for this, which takes care of url building:
Another way of writing it, with short form:
This way you reduce any possibile error with escaping quotes etc, just write plain html and use php as templating engine. Personally, I find this way easier to read and less error prone.