Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Schleife

Empfohlene Antworten

Veröffentlicht

<?php

$wert=64;

for($i=2; $i<=20; $i++)

{

$a=$wert%$i;

if($a==0)

{

$res=$wert/$i;

echo $wert."=".$i."*".$res."<br>";

}

}

?>

Am Browser erscheint

64=2*32

64=4*16

64=8*8

64=16*4

Es soll aber nur der erste Wert angezeigt werden. Was muss ich in der Schleife bzw. if -Anweisung ändern damit nur der erste Wert ausgegeben wird

Gruss Flo

:confused:

Hallo,

da gibt es verschiedene Möglichkeiten.

1. du nimmst ne Status Variable.

2. du setzt im if Block i>20 (also missbrauchen von i als Statusvariable)

3. du schreibst das in eine Funktion und springst mit einem return im if Block raus.

ich würde Methode 3 bevorzugen...

Gruß Jaraz

Sowas in der Art?:

<?php

$wert=64;

for($i=2; $i<=20; $i++)

{

if($i==2) $a=$wert%$i;

if($a==0)

{

$res=$wert/$i;

echo $wert."=".$i."*".$res."<br>";

}

}

?>

Ich hab mich vielleicht falsch ausgedrückt. Ganz allgemein möchte ich aus einer Schleife Werte auslesen aber der "erste" Wert der ausgelesen wird soll in einer Variable gespeichert werden

Gruss Flo

Vielleicht versteh ich Dich ja falsch, aber entweder nimmst Du ne Statusvariable:

<?php

$status = false;

$wert=64;

for($i=2; $i<=20; $i++)

{

$a=$wert%$i;

if(($a==0) && ($status == false))

{

$res=$wert/$i;

echo $wert."=".$i."*".$res."<br>";

$status = true;

}

}

?>

Oder Du läßt die Schleife rückwärts laufen und gibst nicht jedesmal die Zeile aus:

<?php

$wert=64;

for($i=20; $i>=2; $i--)

{

$a=$wert%$i;

if($a==0)

{

$res=$wert/$i;

}

}

echo $wert."=".$i."*".$res."<br>";

?>

Oder du gehst im "if($a==0)"-Zweig aus der Schleife raus.

Sillium

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.