/ Forside / Teknologi / Udvikling / PHP / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
PHP
#NavnPoint
rfh 3959
natmaden 3372
poul_from 3310
funbreak 2700
stone47 2230
Jin2k 1960
Angband 1743
Bjerner 1249
refi 1185
10  Interkril.. 1146
Switch-problemer
Fra : Silas Boye Nissen


Dato : 28-09-05 16:38

Hej

Jeg ønsker at, hvis $land = "Ægypten" så skal $continent =
"africa" og, hvis $land = "Mexico" så skal $continent =
"north_america"
og i alle andre tilfælde skal $continent være = "europe".

Jeg har lavet følgende stump kode:
switch ($land) {
case 'Ægypten': $continent = 'africa'; break;
case 'Mexico': $continent = 'north_america'; break;
default: $continent = 'europe';
}
Men det virker ikke!

Hvad gør jeg forkert?

Venlig hilsen,
Silas Boye Nissen
www.sporvognsrejser.dk

--
Vil du lære at kode HTML, XHTML, CSS, SSI, ASP eller ASP.NET?
- Pædagogiske tutorials på dansk
- Kom godt i gang med koderne
KLIK HER! => http://www.html.dk/tutorials

 
 
Nezar Nielsen (28-09-2005)
Kommentar
Fra : Nezar Nielsen


Dato : 28-09-05 17:19

Silas Boye Nissen wrote:

> Jeg har lavet følgende stump kode:
> switch ($land) {
> case 'Ægypten': $continent = 'africa'; break;
> case 'Mexico': $continent = 'north_america'; break;
> default: $continent = 'europe';
> }

Der er ikke noget galt med den stump kode der, så mon ikke det er fordi
din $land ikke Ægypten eller Mexico (vi ER case-sensitive her.. ).

[root@linux html]# php
<?php
$land = 'Ægypten';
switch ($land) {
case 'Ægypten': $continent = 'africa'; break;
case 'Mexico': $continent = 'north_america'; break;
default: $continent = 'europe';
}
print $continent;

africa




[root@linux html]# php
<?php
$land = 'Danmark';
switch ($land) {
case 'Ægypten': $continent = 'africa'; break;
case 'Mexico': $continent = 'north_america'; break;
default: $continent = 'europe';
}
print $continent;

europe


--
Mvh. Nezar Nielsen
http://gorilla.dk

Bertel Lund Hansen (28-09-2005)
Kommentar
Fra : Bertel Lund Hansen


Dato : 28-09-05 18:10

Silas Boye Nissen skrev:

> Jeg har lavet følgende stump kode:
> [...]
> Men det virker ikke!

> Hvad gør jeg forkert?

Fejlen ligger ikke i den kode du har vist.

PS. Du gør dig selv og andre en tjeneste ved at lave indrykning
og altså skrive sådan her:

switch ($land) {
case 'Ægypten': $continent = 'africa'; break;
case 'Mexico': $continent = 'north_america'; break;
default: $continent = 'europe';
}

--
Bertel
http://bertel.lundhansen.dk/      http://fiduso.dk/

Nygaard (28-09-2005)
Kommentar
Fra : Nygaard


Dato : 28-09-05 18:49

> PS. Du gør dig selv og andre en tjeneste ved at lave indrykning
> og altså skrive sådan her:
>
> switch ($land) {
> case 'Ægypten': $continent = 'africa'; break;
> case 'Mexico': $continent = 'north_america'; break;
> default: $continent = 'europe';
> }
>
> --

Og endnu mere ved at skrive sådan her:

switch ($land) {
case 'Ægypten':
$continent = 'africa';
break;
case 'Mexico':
$continent = 'north_america';
break;
default:
$continent = 'europe';
}

- Men det er selvfølgelig religion....

/Nygaaard



Bertel Lund Hansen (28-09-2005)
Kommentar
Fra : Bertel Lund Hansen


Dato : 28-09-05 19:04

Nygaard skrev:

> Og endnu mere ved at skrive sådan her:

> switch ($land) {
> case 'Ægypten':
> $continent = 'africa';
> break;
> case 'Mexico':
> $continent = 'north_america';
> break;
> default:
> $continent = 'europe';
> }

> - Men det er selvfølgelig religion....

Hvis der kun er ét statement til hver case (plus break), har jeg
en tendens til at skrive det på én linje. Ellers gør jeg som dig.

--
Bertel
http://bertel.lundhansen.dk/      http://fiduso.dk/

Silas Boye Nissen (28-09-2005)
Kommentar
Fra : Silas Boye Nissen


Dato : 28-09-05 20:20

Prøv at se det i sammenhæng:

<?
function linksside($country, $land) {echo
"<a
href='http://www.sporvognsrejser.dk/links/$continent/$country.php
'>$land</a>.";}

switch ($land) {
case 'Ægypten': $continent = 'africa'; break;
case 'Mexico': $continent = 'north_america'; break;
default: $continent = 'europe';
}

$land = "Ægypten";

linksside("egypt", "$land");
?>

Dette resulterer i:
<a
href='Ægypten'>http://www.sporvognsrejser.dk/links//egypt.php'>Ægypten
Hvilket det logisknok ikke skal.

Håb på lidt hjælp.

Venlig hilsen,
Silas
www.sporvognsrejser.dk

--
Vil du lære at kode HTML, XHTML, CSS, SSI, ASP eller ASP.NET?
- Pædagogiske tutorials på dansk
- Kom godt i gang med koderne
KLIK HER! => http://www.html.dk/tutorials

Geert Lund (28-09-2005)
Kommentar
Fra : Geert Lund


Dato : 28-09-05 20:31

Silas Boye Nissen wrote:

> Prøv at se det i sammenhæng:

Du refererer jo en ikke globalt erklæret variabel $continent inde i din
function linksside()...

Brug fx:

echo "<a
href='http://www.sporvognsrejser.dk/links/".$GLOBALS['continent']/$country.php
'>$land</a>.";

eller husk at erklære $continent som GLOBAL i starten af din funktion:

function linksside($country, $land) {
GLOBAL $continent;
echo "<a
href='http://www.sporvognsrejser.dk/links/$continent/$country.php
'>$land</a>.";
}

Eller 3. mulighed - send den med i dit funktionskald.

Check evt. PHP manualen for Scopes - http://dk.php.net/variables.scope


--
Med venlig hilsen
Geert Lund,
www.GLD.dk

Geert Lund (28-09-2005)
Kommentar
Fra : Geert Lund


Dato : 28-09-05 20:35

Geert Lund wrote:

> echo "<a
> href='http://www.sporvognsrejser.dk/links/".$GLOBALS['continent']/$country.php
> '>$land</a>.";

Skulle selvfølgelig være:

echo "<a
href='$land.";'>http://www.sporvognsrejser.dk/links/".$GLOBALS['continent']."/$country.php'>$land.";

--
//Geert

Silas Boye Nissen (29-09-2005)
Kommentar
Fra : Silas Boye Nissen


Dato : 29-09-05 21:49

Geert Lund wrote in dk.edb.internet.webdesign.serverside.php:
> Eller 3. mulighed - send den med i dit funktionskald.

Jeg ville være meget gald, hvis jeg ikke behøvede at gøre variablen GLOBAL.
Derfor vil jeg spørge om du ikke kan give mig et eksempel på den 3. mulighed
eller omskrive situationen til den 3. mulighed?

Venlig hilsen,
Silas
www.sporvognsrejser.dk

--
Vil du lære at kode HTML, XHTML, CSS, SSI, ASP eller ASP.NET?
- Pædagogiske tutorials på dansk
- Kom godt i gang med koderne
KLIK HER! => http://www.html.dk/tutorials

Geert Lund (29-09-2005)
Kommentar
Fra : Geert Lund


Dato : 29-09-05 22:20

Silas Boye Nissen wrote:

> Jeg ville være meget gald, hvis jeg ikke behøvede at gøre variablen GLOBAL.
> Derfor vil jeg spørge om du ikke kan give mig et eksempel på den 3. mulighed
> eller omskrive situationen til den 3. mulighed?

I dette tilfælde kan jeg dog ikke se der er noget der skulle hindre dig
i at benytte $GLOBALS da der så vidt jeg kan se er så mange andre ting
der kunne ske at gå galt - men dig om det

<?php

function linksside( $continent, $country, $land ) {
echo "<a
href='$land";'>http://www.sporvognsrejser.dk/links/$continent/$country.php'>$land";
}

$land = "Ægypten";

switch ($land) {
case 'Ægypten': $continent = 'africa'; break;
case 'Mexico': $continent = 'north_america'; break;
default: $continent = 'europe';
}

linksside( $continent, "egypt", "$land" );

?>

--
Med venlig hilsen
Geert Lund,
www.GLD.dk

Per Thomsen (30-09-2005)
Kommentar
Fra : Per Thomsen


Dato : 30-09-05 16:33

Geert Lund wrote:
> Silas Boye Nissen wrote:
>
[klip]
>
> <?php
>
> function linksside( $continent, $country, $land ) {
> echo "<a
> href='$land";'>http://www.sporvognsrejser.dk/links/$continent/$country.php'>$land";
>
> }
>
> $land = "Ægypten";
>
> switch ($land) {
> case 'Ægypten': $continent = 'africa'; break;
> case 'Mexico': $continent = 'north_america'; break;
> default: $continent = 'europe';
> }
>
> linksside( $continent, "egypt", "$land" );
>
> ?>

Jeg ville nok have gjort sådan her:

function getContinent($country) {
switch( $country ) {
case 'Ægypten':
$continent = 'africa';
break;
case 'Mexico':
$continent = 'north_america';
break;
default:
$continent = 'europe';
}
return $continent;
}

$continent = getContinent( $land );
linkssside( $continent, $country, $land );


MVH Per Thomsen,
<http://www.pert.dk/>

Søg
Reklame
Statistik
Spørgsmål : 177501
Tips : 31968
Nyheder : 719565
Indlæg : 6408526
Brugere : 218887

Månedens bedste
Årets bedste
Sidste års bedste