PHP Function parameters - problem with var not being set - php

So I am obviously not a very good programmer. I have written this small function:
function dispAdjuggler($atts) {
extract(shortcode_atts(array(
'slot' => ''
), $atts));
$adspot = '';
$adtype = '';
// Get blog # we're on
global $blog_id;
switch ($blog_id) {
case 1:
// root blog HOME page
if (is_home()) {
switch ($slot) {
case 'top_leaderboard':
$adspot = '855525';
$adtype = '608934';
break;
case 'right_halfpage':
$adspot = '855216';
$adtype = '855220';
break;
case 'right_med-rectangle':
$adspot = '858222';
$adtype = '613526';
break;
default:
throw new Exception("Ad slot is not defined");
break;
}
When I reference the function on a page like so:
<?php dispAdjuggler("top_leaderboard"); ?>
The switch is throwing the default exception. What am I doing wrong here?
Thanks!!

Without know what the shortcode_atts() function does, it looks like you're passing in array to set default values ('slot' = empty string), which extract() then converts into $short = ''
extract(shortcode_atts(array(
'slot' => ''
), $atts));
right? Now that $slot is the empty string, it won't match any of the cases in your switch, and therefore the default case gets triggered, which throws the "Ad slot is not defined" exception.

Aren't you missing the extract type parameter?
http://us.php.net/manual/en/function.extract.php

Related

Using a switch statement to append URL structure

So I have the following switch statement inside a method inside my class:
public function getSASURL(string $blobName, string $size = '') : string
{
if ($this->containerName === 'userpictures-resized') {
switch ($size) {
case 'small':
return '_SThumb';
break;
case 'medium':
return '_MThumb';
break;
case 'large':
return '_LThumb';
break;
case 'original':
return '_Original';
break;
case 'big':
return '_Big';
break;
}
return $this->generateSASURL($blobName . $size . '.jpg');
}
return $this->generateSASURL($blobName);
}
Here is what it's doing:
When I call:
$sasURL = $azure->getSASURL('AHahn_com');
I get the following thing back - this works as expected.
https://storageaccount.blob.core.windows.net/userpictures-resized/AHahn_com.jpg?sv=2017-11-09&sr=b&st=2020-10-08T13:04:47Z
Now when I call:
$sasURL = $azure->getSASURL('AHahn_com', 'small');
I get the following back:
'_SThumb'
Instead, this is the result that I want to receive back (with _SThumb after the filename, before the extension):
https://storageaccount.blob.core.windows.net/userpictures-resized/AHahn_com_SThumb.jpg?sv=2017-11-09&sr=b&st=2020-10-08T13:04:47Z
Does anyone know what I might be doing wrong?
Your problem is that you return from the switch statement rather than use the value for each case. You can either overwrite $size inside each case, so for example $size = '_SThumb'; instead of return '_SThumb';, or create another variable which you substitute for $size to your generateSASURL() method.
However, another approach could be to create a map of the sizes and their corresponding value. Then just access the array with $size as a key. To make it case-insensitve, you can do $map[strtolower($size)]. The default value (when no match was found) is the value after the null-coalescing operator ??, which could be anything - here it's $size, but it could be an empty string if that's more applicable.
public function getSASURL(string $blobName, string $size = '') : string
{
if ($this->containerName === 'userpictures-resized') {
$map = [
'size' => '_SThumb',
'medium' => '_SThumb',
'large' => '_MThumb',
'original' => '_Original',
'big' => '_Big',
];
return $this->generateSASURL($blobName . ($map[$size] ?? $size) . '.jpg');
}
return $this->generateSASURL($blobName);
}

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 is clicked, I expect to execute the first case in the switch, but the desired variables are not overwritten.
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>
<ul>
<li>Início</li>
<li>Últimas Notícias
<li>Contato</li>
</ul>
</nav>
When you load index.php?post=myFile, you are generating the following element in the $_GET superglobal array:
$_GET = array ("post" => "myFile");
Then $post = empty($_GET['post']) ? '' : $_GET['post']; declares $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.
The else condition is executed. $titulo = 'Post';
Now, if you want the switch-case block to be executed, you must:
Change if (empty($post)) { to 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.
I say this because posts/myFile is not the same string as 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 = empty($_GET['p']) ? '' : $_GET['p']; // I changed 'home' to ''
if ($post != '' || ($post == '' && $pagina != '')) { // I changed the logic

Dynamically output the page title with php

I am trying to output the page title dynamically. I am using induces and this script is withing the header.php the goal is to output the header dynamically using a case/switch statement. here is my code:
<?php $title ;
switch($_SERVER['PHP_SELF']) {
case '/index.php':
$title = 'Home';
break;
case '/about.php':
$title = 'About';
break;
case '/services.php':
$title = 'Services';
break;
case '/portfolio.php':
$title = 'Portfolio';
break;
case '/staff.php':
$title = 'Staff';
break;
case '/contact.php':
$title = 'Contact us';
break;
} ?> <title><?php echo $title ?></title>
I am getting a error telling me my variable $title is not defined?
What i am doing wrong?
In your first line, you have
<?php $title ;
This $title ; shouldn't be there.
And, as Kailash Ahirwar already mentioned, it's always a good idea to provide a default value for your $title:
switch($_SERVER['PHP_SELF']) {
[...]
default:
$title = "Default title goes here";
}
Try to define $title before switch:
$title = "";
in $_SERVER global array, $_SERVER[PHP_SELF] contains full path of file like
/project_name/index.php or /project_name/about.php or /project_name/services.php
Here project_name is name of your project.
replace
case '/index.php'
case '/about.php'
case '/services.php'
....
to
case '/project_name/index.php'
case '/project_name/about.php'
case '/project_name/services.php'
.....
& also initialize $title in start of php file.
<?php $title = "";
switch ($_SERVER['PHP_SELF']) {
case '/project_name/index.php':
$title = 'Home';
break;
case '/project_name/about.php':
$title = 'About';
break;
case '/project_name/services.php':
$title = 'Services';
break;
case '/project_name/portfolio.php':
$title = 'Portfolio';
break;
case '/project_name/staff.php':
$title = 'Staff';
break;
case '/project_name/contact.php':
$title = 'Contact us';
break;
}
?>
for testing purposes
print_r($_SERVER);
and check for $_SERVER(PHP_SELF) value.
The code looks basically fine though you are missing a "default" block to catch anything that is not caught by any of the "case" statements.
though a beginner in php, i sorted this out on my own, and it is very simple and logical, consider title as string and it is initially empty one write on top $title = "";
when including header in home page just after inclusion write the value of title(reassigning value of variable)

How can I get this PHP variable to be determined by the value of another user selected radio button

$door = $_POST["doorType"];
$doorWidth;
if ($door=="Single")
{
$doorWidth = $width;
}
else if ($door=="Double")
{
$doorWidth = $dOneWidth;
}
When I run the page it doesn't recognize the variable $doorWidth?
$doorWidth;
doesn't assign anything. It only returns the variable ... to anything. Doing this PHP is accessing the variable, causing a notice. Write for example:
$doorWidth = NULL; // assigns something (some default value if $door isn't "Signle" nor "Double")
I guess that $door has a value far from Single|Double. This may be caused by another error in your application. You should learn, that you should in any case set a proper default value for a variable if you are about to assign to it from into a conditional statement (like if):
$doorWidth = 'not set!';
if ($door=="Single")
{
$doorWidth = $width;
}
else if ($door=="Double")
{
$doorWidth = $dOneWidth;
}
Further note about the switch statement which has a default: branch:
switch($door) {
case 'Single' :
// do something
break;
case 'Double' :
// do something else
break;
default:
die('$door has a value far from 'Single|Double'. Currently: ' . $door);
}

php if $_GET equals a certain array

I'm trying to use the break concept, but I am also am using some built in PHP functions to only allow certain values into the get function.
$allowedKeys = array(
'route'
);
$options = array(
'chapter_1' => 'chapter_1',
'chapter_2' => 'chapter_2',
'chapter_3' => 'chapter_3'
);
$_GET = array_intersect_key($_GET, array_flip($allowedKeys));
if($_GET[$allowedKeys[0]] && array_key_exists($_GET[$allowedKeys[0]], $options)) {
if($_GET[$allowedKeys[0]] == $options[0]) {
/* This is where I'm trying to see if route=chapter_1 then do something.
The logic I'm trying to write is if the route is chapter_1 then print
out the content from chapter 1 How can determine this? */
echo "Hello";
}
}
Why isn't this code echoing "hello"?
Why make it more complex than it needs to be?
//check you have a route
$route = isset( $_GET['route'] ) ? $_GET['route'] : '';
switch( $route ) {
case 'chapter_1':
//do chapter one stuff
echo 'Chapter 1';
break;
case 'chapter_2':
//do chapter two stuff
echo 'Chapter 2';
break;
default:
echo 'Intro';
}
I'll answer your question directly for you. 'Hello' isn't showing because it is inside an if statement that isn't being triggered because either "if($_GET[$allowedKeys[0]] && array_key_exists($_GET[$allowedKeys[0]], $options))" or "if($_GET[$allowedKeys[0]] == $options[0])" is returning false.

Categories