Convertir des elseif en switch

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Convertir des elseif en switch

par pèere limpinpin » 31 déc. 2008, 00:21

Bonsoir Aureusms,

Ton avis est d'or (pardon pour le jeu de mots avec ton pseudo !).

Oui, oui, j'avais relu la doc Php en me disant qu'il devait y avoir une information, peut-être implicite, que je ne saissais pas (j'avais pensé aux booléens !)...

Grâce à toi, ma connaissance de Php a avancé d'un grand pas, et je vais franchir le seuil de la nouvelle année en me posant une question de moins, c'est déjà ça !

Encore merci,

Bien à toi,

P.L.

par Aureusms » 31 déc. 2008, 00:16

Tu as mélangé les deux les deux notions. Pour faire un switch + case, tu dois tester la variable entre parenthèses. Dans ton cas tu ne peux tester que les valeurs permises de image_location mais tu testes pas les deux autres variables.

En clair un switch + case remplace les esleif seulement pour une variable. Toi tu en testes 3. Tu peux faire des switch en cascade mais cela revient au même que des elseif.

Convertir des elseif en switch

par pèere limpinpin » 30 déc. 2008, 23:58

Bonsoir,

J'ai écrit une suite d'instructions avec des elseif. C'est un peu bêta, et j'aimerai arriver à écrire un switch sur cette base; Les elseif fonctionnent à merveille, mais pas le switch, d'où ma question.

Voici le code avec mes elseif :
<?php if (($image_location !== '') && ($caption_text !== '') && ($slimbox_location !== '')) { ?>
	<div id="cas-1">
	<a
	title="<?php if($slimbox_title !== '') { echo $slimbox_title; } ?>"
	rel="lightbox"
	href="<?php echo $slimbox_location; ?>"
	>
	<img src="<?php echo $image_location; ?>"
	class="<?php if($caption_class !== '') { echo $caption_class; } else { echo "wp-caption aligncenter"; } ?>"
	alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
	title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
	width="<?php if($image_width !== '') { echo $image_width; } ?>"
	height="<?php if($image_height !== '') { echo $image_height; }  ?>"
	/>
	</a>
	<p><?php if($caption_text !== '') { echo $caption_text; } ?></p>
	</div>
<?php }
elseif(($image_location !== '') && ($caption_text !== '') && ($slimbox_location == '')) { ?>
	<div id="cas-2">
	<img src="<?php echo $image_location; ?>"
	class="<?php if($caption_class !== '') { echo $caption_class; } else { echo "wp-caption aligncenter"; } ?>"
	alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
	title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
	width="<?php if($image_width !== '') { echo $image_width; } ?>"
	height="<?php if($image_height !== '') { echo $image_height; }  ?>"
	/>
	<p><?php if($caption_text !== '') { echo $caption_text; } ?></p>
	</div>
<?php }
elseif(($image_location !== '') && ($caption_text == '') && ($slimbox_location !== '')) { ?>
	<div id="cas-3">
	<a
	title="<?php if($slimbox_title !== '') { echo $slimbox_title; } ?>"
	rel="lightbox"
	href="<?php echo $slimbox_location; ?>"
	>
	<img src="<?php echo $image_location; ?>"
	class="<?php if($image_class !== '') { echo $image_class; } else { echo "aligncenter"; } ?>"
	alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
	title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
	width="<?php if($image_width !== '') { echo $image_width; } ?>"
	height="<?php if($image_height !== '') { echo $image_height; }  ?>"
	/>
	</a>
	</div>
<?php }
elseif(($image_location !== '') && ($caption_text == '') && ($slimbox_location == '')) { ?>
	<div id="cas 4">
	<img src="<?php echo $image_location; ?>"
	class="<?php if($image_class !== '') { echo $image_class; } else { echo "alignleft"; } ?>"
	alt="<?php if($image_alt !== '') { echo $image_alt;} ?>"
	title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
	width="<?php if($image_width !== '') { echo $image_width; } ?>"
	height="<?php if($image_height !== '') { echo $image_height; }  ?>"
	/>
	</div>
<?php }
else { echo ''; }
?>
(Oui, c'est un peu long, mais on peut oublier les instructions en Html !)

Voici le code correspondant que j'essaye d'utiliser avec la condition switch :
<?php
switch ($image_location)
{
case (($image_location !== '') && ($caption_text !== '') && ($slimbox_location !== '')):
{ ?>
	<div id="cas-1">
	<a
	title="<?php if($slimbox_title !== '') { echo $slimbox_title; } ?>"
	rel="lightbox"
	href="<?php echo $slimbox_location; ?>"
	>
	<img src="<?php echo $image_location; ?>"
	class="<?php if($caption_class !== '') { echo $caption_class; } else { echo "wp-caption aligncenter"; } ?>"
	alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
	title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
	width="<?php if($image_width !== '') { echo $image_width; } ?>"
	height="<?php if($image_height !== '') { echo $image_height; }  ?>"
	/>
	</a>
	<p><?php if($caption_text !== '') { echo $caption_text; } ?></p>
	</div>
<?php }
;
break;
case ($image_location !== '') && ($caption_text !== '') && ($slimbox_location == '')): 
{ ?>
	<div id="cas-2">
	<img src="<?php echo $image_location; ?>"
	class="<?php if($caption_class !== '') { echo $caption_class; } else { echo "wp-caption aligncenter"; } ?>"
	alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
	title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
	width="<?php if($image_width !== '') { echo $image_width; } ?>"
	height="<?php if($image_height !== '') { echo $image_height; }  ?>"
	/>
	<p><?php if($caption_text !== '') { echo $caption_text; } ?></p>
	</div>
<?php }
;
break;
case (($image_location !== '') && ($caption_text == '') && ($slimbox_location !== '')):
{ ?>
	<div id="cas-3">
	<a
	title="<?php if($slimbox_title !== '') { echo $slimbox_title; } ?>"
	rel="lightbox"
	href="<?php echo $slimbox_location; ?>"
	>
	<img src="<?php echo $image_location; ?>"
	class="<?php if($image_class !== '') { echo $image_class; } else { echo "aligncenter"; } ?>"
	alt="<?php if($image_alt !== '') { echo $image_alt; } ?>"
	title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
	width="<?php if($image_width !== '') { echo $image_width; } ?>"
	height="<?php if($image_height !== '') { echo $image_height; }  ?>"
	/>
	</a>
	</div>
<?php }
;
break;
case (($image_location !== '') && ($caption_text == '') && ($slimbox_location == '')): 
{ ?>
	<div id="cas 4">
	<img src="<?php echo $image_location; ?>"
	class="<?php if($image_class !== '') { echo $image_class; } else { echo "alignleft"; } ?>"
	alt="<?php if($image_alt !== '') { echo $image_alt;} ?>"
	title="<?php if($image_title !== '') { echo $image_title; } else { echo $image_alt; } ?>"
	width="<?php if($image_width !== '') { echo $image_width; } ?>"
	height="<?php if($image_height !== '') { echo $image_height; }  ?>"
	/>
	</div>
<?php }
;
break;
default: { echo ''; }
}
?>
Quand je teste ce switch, on dirait que Php s'arrête au premier cas, et je ne comprends pas pourquoi ! :evil:

Merci de votre aide et de votre disponibilité,

Cordialement,

P.L.