Why won't my switch statement generate the correct results?

Why won't my switch statement generate the correct results?



I am using a condition and a switch to determine some default values for a few variables, but I am not getting the results that I expect.



When the link <a href="index.php?post=myFile"></a> is clicked, I expect to execute the first case in the switch, but the desired variables are not overwritten.


<a href="index.php?post=myFile"></a>



This is my code that are into my index.php:


$titulo = '';
$keywords = '';
$descricao = '';
$post = empty($_GET['post']) ? '' : $_GET['post'];
$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];

if (empty($post))
switch ($pagina):
case 'posts/myFile':
$titulo = 'this variable doesnot change the value on this file';
$keywords = 'this variable doesnot change the value on this file';
$descricao = 'this variable doesnot change the value on this file';
break;
case 'privacidade':
$titulo = 'Privacidade ';
break;
case 'ultimasnoticias':
$titulo = 'Ultimas Noticias';
break;
default:
$titulo = 'Home';
$pagina = 'home';
endswitch;
else
$titulo = 'Post';



My current results are:


$_GET["post"] = "myFile";
$titulo = "Post";
$keywords = "";
$descricao = "";
$pagina = "home";



My desired results are:


$_GET["post"] = "myFile";
$titulo = 'this variable doesnot change the value on this file';
$keywords = 'this variable doesnot change the value on this file';
$descricao = 'this variable doesnot change the value on this file';
$pagina = "home";



Why am I not able to update the variables using the first case statement?



Edit:



enter image description here



This is the code that are into my index.php to the nav bar:


nav bar


<nav>
<ul>
<li><a href="?p=home">Início</a></li>
<li><a href="?p=ultimasnoticias">Últimas Notícias</a>
<li><a href="?p=contato">Contato</a></li>
</ul>
</nav>





Double Check $post value. is it coming empty or not ?
– amku91
Aug 30 at 6:18




1 Answer
1



When you load index.php?post=myFile, you are generating the following element in the $_GET superglobal array:


index.php?post=myFile


$_GET


$_GET = array ("post" => "myFile");



Then $post = empty($_GET['post']) ? '' : $_GET['post']; declares $post = "myFile".


$post = empty($_GET['post']) ? '' : $_GET['post'];


$post = "myFile"



This means that if (empty($post)) { evaluates to false (because it is not empty, it is myFile) and the switch-case block is ignored.


if (empty($post)) {


false


myFile


switch-case



The else condition is executed. $titulo = 'Post';


else


$titulo = 'Post';



Now, if you want the switch-case block to be executed, you must:


if (empty($post)) {


if (!empty($post)) {



As for future trouble you may have within the switch-case block, make sure that you are identically matching the value for $pagina when writing each case. If you aren't sure about what values you are working with or what is being executed, just add echoes in each case to clarify.


$pagina



I say this because posts/myFile is not the same string as myFile.


posts/myFile


myFile



After more discussion about other hyperlinks, I'll recommend changing the default value for $pagina and extending the if logic to be more inclusive.


$pagina


if


$pagina = empty($_GET['p']) ? '' : $_GET['p']; // I changed 'home' to ''

if ($post != '' || ($post == '' && $pagina != '')) { // I changed the logic



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế